-1

just trying to get hold on EF. when we work with sql then we often write multiple value inside in clause

Select * from customer
Where countryCode in ('gb','us','fr')

i was searching how to write the same query with EF and LINQ. i found these code.

var countries= new[] {
    new {Country=…, City=…, Address=…},
    …
}

approach 1
------------
var result = locations.Where(l => keys.Any(k => 
                    k.Country == l.Country && 
                    k.City == l.City && 
                    k.Address == l.Address));

approach 2
------------
var result = from loc in Location
             where keys.Contains(new {
                 Country=loc.Country, 
                 City=loc.City, 
                 Address=loc.Address
             }
             select loc;

tell me how to translate below sql query to EF without using multiple contains keyword

Select * from customer
Where countryCode in ('gb','us','fr')
Rob
  • 26,989
  • 16
  • 82
  • 98
Monojit Sarkar
  • 2,353
  • 8
  • 43
  • 94

1 Answers1

2

If i understand you correctly, you can just do this

var countryCodes = new List<string> {"gb","us","fr"}

var locations = Location.Where(loc => countryCodes.Contains(loc.Country));
TheGeneral
  • 79,002
  • 9
  • 103
  • 141