【发布时间】:2013-10-09 16:26:18
【问题描述】:
我正在尝试从一个实体中获取所有不加入另一个实体的记录。
这就是我在 SQL 中尝试做的事情:
SELECT * from table1
LEFT join table2
ON table1.code = table2.code
WHERE table2.code IS NULL
这会导致 table1 的所有行都没有连接到 table2。
我在加入一个字段时使用 Linq,但我有联系人记录要加入名字、出生日期和号码。
我有一个导入到的“暂存”实体;工作流处理暂存记录并创建新联系人。
暂存实体几乎是真实实体的副本。
var queryable = from staging in linq.mdo_staging_contactSet
join contact in linq.ContactSet
on staging.mdo_code equals contact.mdo_code
into contactGroup
from contact in contactGroup.DefaultIfEmpty()
// all staging records are selected, even if I put a where clause here
select new Contact
{
// import sequence number is set to null if the staging contact joined to the default contact, which has in id of null
ImportSequenceNumber = (contactContactId == null) ? new int?(subImportNo) : null,
/* other fields get populated */
};
return queryable // This is all staging Contacts, the below expressions product only the new Contacts
.AsEnumerable() // Cannot use the below query on IQuerable
.Where(contact => contact.ImportSequenceNumber != null); // ImportSequenceNumber is null for existing Contacts, and not null for new Contacts
我可以使用方法语法做同样的事情吗?
我可以执行上述操作并加入多个领域吗?
我发现的替代方案更糟,涉及使用 newRecords.Except(existingRecords),但使用 IEnumerables;有没有更好的办法?
【问题讨论】:
-
我自己只是在调查这个。 This answer 似乎表明 CRM 的 LINQ 实现是不可能的。请参阅链接的 MSDN doco 的 limitations 部分说
You cannot perform outer joins