如何进行LINQ to Entities查询"除外"?

Sli*_*ggy 9 linq-to-entities entity-framework-4

我在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);
Run Code Online (Sandbox Code Playgroud)

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

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

Sli*_*ggy 12

看起来我找到了解决方案:

var paymentSystems = pge.PaymentSystems.Where(
    item => !item.Accounts.Any(t => t.id == accountId));
Run Code Online (Sandbox Code Playgroud)

似乎可以做到这一点.