Using LINQ to remove elements from a List

前端 未结 14 2120
抹茶落季
抹茶落季 2020-11-22 11:09

Say that I have LINQ query such as:

var authors = from x in authorsList
              where x.firstname == \"Bob\"
              select x;

14条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-22 11:31

    You can remove in two ways

    var output = from x in authorsList
                 where x.firstname != "Bob"
                 select x;
    

    or

    var authors = from x in authorsList
                  where x.firstname == "Bob"
                  select x;
    
    var output = from x in authorsList
                 where !authors.Contains(x) 
                 select x;
    

    I had same issue, if you want simple output based on your where condition , then first solution is better.

提交回复
热议问题