none
Exclude elements of Array in list using LINQ

    Question

  • I want to exclude elements of array in List that matches with specific criteria using LINQ, any one has idea on how to achieve this?
    Friday, January 03, 2014 10:28 AM

Answers

  • Hope this for loop helps as start between your input and output stmt

      foreach(Class1 el in InputList)
               {
                   if (el.Array !=null && el.Array.Select(x => x == "AAA").Count() > 0)
                   {
                       var occurs = el.Array.Where(x => x == "AAA").ToArray();
                       el.Array = el.Array.Except(occurs).ToArray();
                   }
               
                }

    Monday, January 06, 2014 2:54 PM
  • Hello KANTH.P,

    Have a try to use LINQ2Object like below:

    //Input
    
                List<Class1> InputList = new List<Class1> { 
    
                    new Class1 { ID = 1, Array = new string[] { "AAA", "BBB", "CCC" } }, 
    
                    new Class1 { ID = 2, Array = new string[] { "DDD", "AAA", "KKK", "EEE" } }, 
    
                    new Class1 { ID = 3, Array = null }, 
    
                    new Class1 { ID = 4, Array = new string[] { "QQQ" }, } };
    
    
                //Output
    
                List<Class1> OutputList = InputList.Select(c1 => new Class1() { ID = c1.ID, Array = c1.Array == null ? null : c1.Array.Where(a => a.ToString() != "AAA").ToArray() }).ToList();
    

    If you want to exclude other element, you just need to replace “AAA” with others.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, January 08, 2014 6:26 AM
    Moderator

All replies

  • You can use Where on Linq to get only the items that you require. For example you have a string array and you want only to get the strings that have length equal to 3.

    string[] array = { "dot", "", "net", null, null, "perls", null };
    var output = array.Where(a => (a != null && a.Length == 3));
    
    foreach (string value in output)
    {
        Console.WriteLine(value);
    }
    

    You will get output:
    dot
    net


    Kunal G

    Friday, January 03, 2014 12:19 PM
  • Hi Kunal G,

    Below is the sample of Input and out put list, I want to exclude specific elements from array in each list Item.

    //Input
    List<Class1> InputList = new List<Class1>{new Class1{ ID=1, Array= new string[]{"AAA", "BBB", "CCC"}},
         new Class1{ ID=2, Array= new string[]{ "DDD", "AAA", "KKK", "EEE" }},
         new Class1{ ID=3, Array= null},
         new Class1{ ID=4, Array= new string[]{ "QQQ"}, }
                                                                                };

    //Output
    List<Class1> OutputList = new List<Class1>{new Class1{ ID=1, Array= new string[]{"BBB", "CCC"}},
                new Class1{ ID=2, Array= new string[]{ "DDD", "KKK", "EEE" }},
                new Class1{ ID=3, Array= null},
                new Class1{ ID=4, Array= new string[]{ "QQQ"}, }
                                                                                       };

    Monday, January 06, 2014 8:17 AM
  • Hope this for loop helps as start between your input and output stmt

      foreach(Class1 el in InputList)
               {
                   if (el.Array !=null && el.Array.Select(x => x == "AAA").Count() > 0)
                   {
                       var occurs = el.Array.Where(x => x == "AAA").ToArray();
                       el.Array = el.Array.Except(occurs).ToArray();
                   }
               
                }

    Monday, January 06, 2014 2:54 PM
  • Hello KANTH.P,

    Have a try to use LINQ2Object like below:

    //Input
    
                List<Class1> InputList = new List<Class1> { 
    
                    new Class1 { ID = 1, Array = new string[] { "AAA", "BBB", "CCC" } }, 
    
                    new Class1 { ID = 2, Array = new string[] { "DDD", "AAA", "KKK", "EEE" } }, 
    
                    new Class1 { ID = 3, Array = null }, 
    
                    new Class1 { ID = 4, Array = new string[] { "QQQ" }, } };
    
    
                //Output
    
                List<Class1> OutputList = InputList.Select(c1 => new Class1() { ID = c1.ID, Array = c1.Array == null ? null : c1.Array.Where(a => a.ToString() != "AAA").ToArray() }).ToList();
    

    If you want to exclude other element, you just need to replace “AAA” with others.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, January 08, 2014 6:26 AM
    Moderator