I’m concerned that this LINQ call actually makes two trips to the database (once for Contains, once for ToList), when all I really want is the SQL-equivalent of a nested select statement:
var query1 = from y in e.cities where y.zip=12345 select y.Id;
var query2 = from x in e.users where query1.Contains(x.cityId) select x;
List<users> result = query2.ToList();
The point: If this is making a trip to the database twice, how do I avoid that? How can I have a nested select statement like this that will just execute as one query one time? Query1 will only ever return 1 or 0 rows. There must be a better way than using “Contains”.
Since
query1andquery2are bothIQueryablethere is only one trip to the database – when you callquery2.ToList()You could combine the queries using a join since you are looking for related information and the relationship is that the user’s city id is the same as the city you are restricting to:
Above should give you a list of user ids of users that (presumably) live in the zip code 12345.