Entity Framework - Condition on one to many join (Lambda)
Posted
by nirpi
on Stack Overflow
See other posts from Stack Overflow
or by nirpi
Published on 2010-05-07T10:32:26Z
Indexed on
2010/05/07
10:38 UTC
Read the original article
Hit count: 1122
Hi,
I have 2 entities: Customer & Account, where a customer can have multiple accounts. On the account, I have a "PlatformTypeId" field, which I need to condition on (multiple values), among other criterions. I'm using Lambda expressions, to build the query. Here's a snippet:
var customerQuery = (from c in context.CustomerSet.Include("Accounts")
select c);
if (criterions.UserTypes != null && criterions.UserTypes.Count() > 0)
{
List<short> searchCriterionsUserTypes = criterions.UserTypes.Select(i => (short)i).ToList();
customerQuery = customerQuery.Where(CommonDataObjects.LinqTools.BuildContainsExpression<Customer, short>(c => c.UserTypeId, searchCriterionsUserTypes));
}
// Other criterions, including the problematic platforms condition (below)
var customers = customerQuery.ToList();
I can't figure out how to build the accounts' platforms condition:
if (criterions.Platforms != null && criterions.Platforms.Count() > 0)
{
List<short> searchCriterionsPlatforms = criterions.Platforms.Select(i => (short)i).ToList();
customerQuery = customerQuery.Where(c => c.Accounts.Where(LinqTools.BuildContainsExpression<Account, short>(a => a.PlatformTypeId, searchCriterionsPlatforms)));
}
(The BuildContainsExpression is a method we use to build the expression for the multi-select)
I'm getting a compilation error:
The type arguments for method 'System.Linq.Enumerable.Where(System.Collections.Generic.IEnumerable, System.Func)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
Any idea how to fix this?
Thanks,
Nir.
© Stack Overflow or respective owner