【问题标题】:Dynamics CRM 2011 Linq Left Outer JoinDynamics CRM 2011 Linq 左外连接
【发布时间】: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

标签: linq dynamics-crm-2011


【解决方案1】:

您可以对方法调用做同样的事情,但有些人往往会发现它更难阅读,因为中间有一些 LAMBDA 表达式。 Here 是一个例子,说明两者基本相同。

我看到其他人提出同样的问题,这归结为开发人员的选择。我个人喜欢 LINQ 方法,因为我还写了一堆 SQL,而且我可以更轻松地阅读代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-28
    相关资源
    最近更新 更多