LINQ to Entities - where..in clause with multiple columns

后端 未结 12 2063
别那么骄傲
别那么骄傲 2020-12-05 18:23

I\'m trying to query data of the form with LINQ-to-EF:

class Location {
    string Country;
    string City;
    string Address;
    …
}

by

12条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-05 19:13

    I'd replace Contains (which is a method specific to lists and arrays) with the wider IEnumerable's Any extension method:

    var result = Location
        .Where(l => keys.Any(k => l.Country == k.Country && l.City = k.City && l.Address == k.Address);
    

    This can also be written:

    var result = from l in Location
                 join k in keys
                 on l.Country == k.Country && l.City == k.City && l.Address == k.Address
                 select l;
    

提交回复
热议问题