2 Entities, "in" clause?

Posts   
 
    
ianvink
User
Posts: 394
Joined: 15-Dec-2006
# Posted on: 22-Jan-2010 18:52:13   

Here's a simplified problem I'm having.

I have :

countries=  EntityCollection<CountryEntity>

from one data source populated.

I need to get a list of States from another data source that match state.CountryFK = country.CountryPK in Linq

This fails:

            var q = (ILLBLGenProQuery) (from x in linq.SecondsDataSourceCities
                                        where countries.First(e=>e.CountryPK ==x.CountryFK )!=null
                                        select x);

How do I do an 'in' clause?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 23-Jan-2010 00:20:29   

This is how:

  1. Extract the id values from countries in-memory.
  2. Use contains to generate IN CLAUSE in query
  3. Execute the final query.

Example:

// this is in-memory, just to extract Id's
List<string> cids = countries.Select(c => c.CountryId).ToList();

// create the query
var q2 = from s in metaData.SecondsDataSourceCities
         where cids.Contains(s.CountryId)
         select s;

// fetch
List<OrdersEntity> orders = q2.ToList();
David Elizondo | LLBLGen Support Team