In condition using LINQ

后端 未结 2 828
囚心锁ツ
囚心锁ツ 2021-01-22 07:07

OK, another LINQ question. How do I do an \"IN\" condition using LINQ. I have an IEnumerable list of myObject and want to do something like myObject.Description in(\'Help\',

相关标签:
2条回答
  • 2021-01-22 08:00

    Use Contains on a collection:

    string[] descriptions = { "Help", "Admin", "Docs" };
    
    var query = from foo in list
                where descriptions.Contains(foo.Description)
                select ...;
    

    (For larger collections, a HashSet<T> might be better.)

    0 讨论(0)
  • 2021-01-22 08:04

    IN in sql is equivalent is Contains in LINQ

    string[] countries = new string[] { "UK", "USA", "Australia" };
    var customers =
        from c in context.Customers
        where countries.Contains(c.Country)
        select c;
    
    0 讨论(0)
提交回复
热议问题