【问题标题】:What Is the Difference Between These Two Queries Where One Throws System.NotSupportedException抛出 System.NotSupportedException 的这两个查询有什么区别
【发布时间】:2013-09-26 08:36:23
【问题描述】:

我遇到了一个在此网站上搜索良好的异常:

System.NotSupportedExceptionLocal 序列不能在查询运算符的 LINQ to SQL 实现中使用,但 Contains 运算符除外。

我发现this 的回答很有帮助。然而,看似矛盾的答案,这个查询对我来说很好:

return clients.Where(client => 
    client.CompanyID.HasValue && 
    client.Company.CompanyAssociations.Any(ass => 
       localListOfSearchStrings.Any(str => ass.Contact.FullName.Contains(str))));

虽然这个查询在我看来完全一样,但会引发异常:

return stores.Where(store => 
    store.Asset.AssetOwners.Any(assetOwner => 
        assetOwner.Client.CompanyID.HasValue && 
        assetOwner.Client.Company.CompanyAssociations.Any(ass =>
            localListOfSearchStrings.Any(str => 
                ass.Contact.FullName.Contains(str)))));

我不想将记录列表拉入本地内存进行评估。

另一个复杂的问题是localListOfSearchStrings 可能包含名字或姓氏。

所以我真的需要评估

localListOfSearchStrings.Any(str => 
    CompanyAssociation.Contact.FullName.Contains(str))

而不是,例如,

listOfLocalSearchStrings.Contains(CompanyAssociation.Contact.FullName)

【问题讨论】:

  • 看到异常消息,令人惊讶的是第一个查询运行良好。看起来您需要一个解决方案来构建“OR”查询,例如使用 PredicateBuilder。或者转移到实体框架:)

标签: c# linq-to-sql


【解决方案1】:

当然,答案很愚蠢。

cited above 的答案是正确的。查询正常工作的原因是因为clients 在调用查询之前实际上已被强制转换为列表 - SQL Server Profiler 提醒我这一事实。这太糟糕了。

正如 Gert Arnold 上面指出的那样,该问题的解决方案是使用 PredicateBuilder(在引用的问题中也提出了建议)。

【讨论】:

    猜你喜欢
    • 2010-12-19
    • 2017-04-19
    • 1970-01-01
    • 1970-01-01
    • 2013-04-11
    • 2012-04-19
    • 1970-01-01
    相关资源
    最近更新 更多