实体框架 - 一对多连接的条件(Lambda)
我有 2 个实体:客户和实体帐户,一个客户可以拥有多个帐户。 在帐户上,我有一个“PlatformTypeId”字段,除其他条件外,我还需要以该字段为条件(多个值)。 我正在使用 Lambda 表达式来构建查询。这是一个片段:
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(LinqTools.BuildContainsExpression<Customer, short>(
c => c.UserTypeId, searchCriterionsUserTypes));
}
// Other criterions, including the problematic platforms condition (below)
var customers = customerQuery.ToList();
我无法弄清楚如何构建帐户的平台条件:
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)));
}
(BuildContainsExpression 是我们用来构建多选表达式的方法)
我收到编译错误:
方法的类型参数无法从用法推断“System.Linq.Enumerable.Where(System.Collections.Generic.IEnumerable, System.Func)”。尝试显式指定类型参数。
知道如何解决这个问题吗?
谢谢,
尼尔。
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(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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想这可能会为你解决这个问题。
这将过滤拥有其 platfortypeid 包含在 searchCriterionsPlatforms 列表中的任何帐户的客户。
HTH
PS。
您可以将: 更改
为:
IMO,这样更干净。
I think this might fix it for you.
This will filter on customers who have any account that whose platfortypeid is contained in the searchCriterionsPlatforms list.
HTH
ps.
You can change:
to:
IMO, that is much cleaner.