-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')
3

1 Answer 1

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));
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.