动态CRM Linq查询加入此,那或其他

时间:2018-02-28 01:32:47

标签: linq dynamics-crm

我想做什么

我有两个实体

ENTITY1

Name: MyEntity
Attribute1: Entity2 Reference
Attribute2: Entity2 Reference
Attribute3: Entity2 Reference

实体2

Name: My Other Entity

实体1的Attribute1,Attribute2和Attribute3是可选的,因此它们也可以为空。

我需要一个linq查询,它将从实体1的Attribute1,Attribute2和Attribute3返回实体2记录。 我不希望引用它们,因为这意味着我必须进行另一次数据库查找。

示例

var rows = (from ent1 in crmLinqContext.Entity1Set
            from ent2 in crmLinqContext.Entity2Set
            where ent1.attribute1 == ent2.Id ||
                  ent1.attribute2 == ent2.Id ||
                  ent1.attribute3 == ent2.Id
                  select new entity2 
                  {
                     Id = ent2.Id,
                     Name = ent2.Name
                  });

问题

我收到错误" A' SelectMany'操作必须先在' Where'过滤的操作 通过实体ID。"

问题

如何使用来自所有三个可能属性的XrmContext加入

更新1

var rows = (from ent1 in crmLinqContext.Entity1Set
                     join ent2_1 in crmLinqContext.Entity2Set 
                     on ent1.attribute1.Id equals ent2_1.Id into et1
                     join ent2_2 in crmLinqContext.Entity2Set
                     on ent1.attribute2.Id equals ent2_2.Id into et2
                     join ent2_3 in crmLinqContext.Entity2Set
                     on ent1.attribute3.Id equals ent2_3.Id into et3
                     from result1 in et1.DefaultIfEmpty()
                     from result2 in et2.DefaultIfEmpty()
                     from result3 in et3.DefaultIfEmpty()
                     select new 
                     {
                         Results = new List<Entity2>()
                         {
                             result1,
                             result2,
                             result3
                         }
                     });

这给了我错误 &#39; GroupJoin&#39;操作必须遵循“SelectMany”&#39;集合选择器正在调用&#39; DefaultIfEmpty&#39;方法

0 个答案:

没有答案