CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Threaded View

  1. #5
    Join Date
    Apr 2014
    Location
    in northeast ohio
    Posts
    94

    Re: LINQ query to remove a single value from a set while retaining another value

    well im no expert
    i spent some time attempting to do this using linq but couldn't find a way

    at the end of my efforts i think

    your original solution is better
    if its over a network there are other considerations though

    i did find a excellent linq reference page though
    http://code.msdn.microsoft.com/101-L...mples-3fb9811b

    on further thought a delete is a request
    rather then a query so it sort of makes sense
    not to be able to do so within a query

    maybe in the first place just query all non or delete-able items
    and remove any sort of delete button when they are worked on
    Code:
            static void Main(string[] args)
            {
                Program I = new Program();
                I.TestLinq01();
            }
            public void TestLinq01()
            {
                listOfuInts = new List<uint>{ 1, 2, 3, 4, 5 };
                ListToConsole("via linq before", listOfuInts);
    
                var query = // var is of type ienumerable from linq
                    from n in listOfuInts  // loop
                    where n != specialValue // conditional
                    select n; // action
    
                listOfuInts = query.ToList<uint>(); // convert back to uint
                
                ListToConsole("after", listOfuInts);
            }
            public void ListToConsole<T>(string msg,List<T> list)
            {
                Console.WriteLine(Environment.NewLine + msg);
                foreach (var obj in list)
                {
                    Console.WriteLine(obj);
                }
                Console.WriteLine(Environment.NewLine);
            }
    the real pain is that the enumerable changes when you perform a delete

    this was my looping solution for a delete all but special values
    in truth i don't really use linq much i find it far more difficult
    then just straight coding something
    Code:
            // remove all not with a special value
            public void TestLoop02()
            {
                listOfuInts = new List<uint> { 1, 2, 3, 4, 5 };
                ListToConsole("via loop02 before", listOfuInts);
                int index = 0;
                while (index < listOfuInts.Count)
                {
                    if (listOfuInts.ElementAt(index) != specialValue)
                    {
                        listOfuInts.RemoveAt(index);
                    }
                    else
                    {
                        index++;
                    }
                }
                ListToConsole("via loop02 after remaining values not removed", listOfuInts);
            }
    but linq is more for querys so...
    Last edited by willmotil; August 23rd, 2014 at 08:32 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured