【问题标题】:How do I make an "Except" LINQ to Entities query?如何进行“除外”LINQ to Entities 查询?
【发布时间】:2010-09-08 11:34:32
【问题描述】:

我在 Accounts 和 PaymentSystems 之间存在多对多关系。我想列出所有尚未分配给帐户的 PaymentSystems。为此,我尝试使用以下 LINQ to Entities 查询:

PaymentGatewayEntities pge = new PaymentGatewayEntities();
Account account = pge.Accounts.Single(item => item.id == accountId);
var paymentSystems = pge.PaymentSystems.Except(account.PaymentSystems);

但是,在尝试显示结果时出现以下异常:“System.NotSupportedException:无法创建类型为“MyNamespace.Models.PaymentSystem”的常量值。只有原始类型(例如 Int32、String 和Guid') 在这种情况下得到支持。”我究竟做错了什么?我正在使用 EF4。

UPD: var paymentSystems = pge.PaymentSystems.Where(item => !item.Accounts.Contains(account)) 也会导致相同的异常。

【问题讨论】:

    标签: linq-to-entities entity-framework-4


    【解决方案1】:

    看来我找到了解决办法:

    var paymentSystems = pge.PaymentSystems.Where(
        item => !item.Accounts.Any(t => t.id == accountId));
    

    似乎可以解决问题。

    【讨论】:

      【解决方案2】:

      相同答案的稍微不同的变体:

      var paymentSystems = pge.PaymentSystems.Where(
          item => item.Accounts.All(t => t.accountId != t.ID));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-07
        相关资源
        最近更新 更多