实体框架 - 一对多连接的条件(Lambda)

发布于 2024-08-31 20:37:43 字数 1405 浏览 3 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无声静候 2024-09-07 20:37:43

我想这可能会为你解决这个问题。

if (criterions.Platforms != null && criterions.Platforms.Count() > 0) 
{ 
    List<short> searchCriterionsPlatforms = 
        criterions.Platforms.Select(i => (short)i).ToList(); 

    customerQuery = customerQuery 
        .Where(c => c.Accounts  
                     .Any(a => searchCriterionsPlatforms 
                               .Contains(a.PlatformTypeId) 
                         )
              );        
} 

这将过滤拥有其 platfortypeid 包含在 searchCriterionsPlatforms 列表中的任何帐户的客户

HTH

PS。

您可以将: 更改

customerQuery = customerQuery 
    .Where(LinqTools.BuildContainsExpression<Customer, short>( 
        c => c.UserTypeId, searchCriterionsUserTypes)); 

为:

customerQuery = customerQuery
                .Where(c => searchCriterionsUserTypes.Contains(c.UserTypeId));

IMO,这样更干净。

I think this might fix it for you.

if (criterions.Platforms != null && criterions.Platforms.Count() > 0) 
{ 
    List<short> searchCriterionsPlatforms = 
        criterions.Platforms.Select(i => (short)i).ToList(); 

    customerQuery = customerQuery 
        .Where(c => c.Accounts  
                     .Any(a => searchCriterionsPlatforms 
                               .Contains(a.PlatformTypeId) 
                         )
              );        
} 

This will filter on customers who have any account that whose platfortypeid is contained in the searchCriterionsPlatforms list.

HTH

ps.

You can change:

customerQuery = customerQuery 
    .Where(LinqTools.BuildContainsExpression<Customer, short>( 
        c => c.UserTypeId, searchCriterionsUserTypes)); 

to:

customerQuery = customerQuery
                .Where(c => searchCriterionsUserTypes.Contains(c.UserTypeId));

IMO, that is much cleaner.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文