How do i get the difference in two lists in C#?

后端 未结 4 2047
广开言路
广开言路 2020-12-10 04:15

Ok so I have two lists in C#

List attributes = new List();
List songs = new List();
         


        
4条回答
  •  春和景丽
    2020-12-10 05:14

    This is the way to find all the songs which aren't included in attributes names:

    var result = songs
      .Where(!attributes.Select(a => a.name).ToList().Contains(song));
    

    The answer using Except is also perfect and probably more efficient.

    EDIT: This sintax has one advantage if you're using it in LINQ to SQL: it translates into a NOT IN SQL predicate. Except is not translated to anything in SQL. So, in that context, all the records would be recovered from the database and excepted on the app side, which is much less efficient.

提交回复
热议问题