LINQ to CRM一对多结果没有扁平化

时间:2014-08-07 15:29:46

标签: c# linq dynamics-crm-2011 dynamics-crm

我尝试使用LINQ to CRM来获取父/子格式的数据。这是一对多,我不希望儿童记录与父母一起变平。首先是我要问的可能吗?

我没有导航属性,会有很多家长返回,反过来每个家长会有很多孩子。

我试图避免多次查询,只需在1中获取。

所以我在之后:

因此父级有很多子级,子级在其他表1到1中有一些其他查找属性。

父 - 儿童+其他信息 - 儿童+其他信息

父 - 儿童+其他信息

父 - 儿童+其他信息 - 儿童+其他信息 - 儿童+其他信息

我已经尝试首先将详细信息作为IQueryable,然后使用标题加入:

var detailsOnly = (from det in db.details
                   join inc in db.Incidents on det.detOtherId equals inc.incidentid into incidentsLo
                    from subInc in incidentsLo.DefaultIfEmpty()
                   join cli in db.Accounts on subInc.accountid equals cli.accountid into accountsLo
                    from subCli in accountsLo.DefaultIfEmpty()

                   select new
                   {
                       det,
                       AccName = subAcc.name, 
                       AccRef = subAcc.accountnumber,
                       IncidentTicketNumber = subInc.ticketnumber,
                       IncidentKeyDescription = subInc.title,
                       IncidentMainContact = subInc.maincontactname
                    });

var query = from head in db.headers
            where head.IsDone == isdone & head.type == typeId & head.accountid == AccountId
            select new MyHeader(head)
            {
                MyDetails = detailsOnly.Where(md => md.det.detOtherId == head.headOtherId)
                .Select(d => new MyDetail(d.det)
                {
                    AccName = d.AccName,
                    AccRef = d.AccRef,
                    IncidentTicketNumber = d.IncidentTicketNumber,
                    IncidentKeyDescription = d.IncidentKeyDescription,
                    IncidentMainContact = d.IncidentMainContact
                }).ToList()
            };

var result = query.ToList();

1 个答案:

答案 0 :(得分:0)

这看起来并没有得到真正的答案,所以我能说的最好的是执行细节收集部分,对其进行分组,然后从中选择其他部分。

var detailsOnly = (from det in db.details
                   join inc in db.Incidents on det.detOtherId equals inc.incidentid into incidentsLo
                    from subInc in incidentsLo.DefaultIfEmpty()
                   join cli in db.Accounts on subInc.accountid equals cli.accountid into accountsLo
                    from subCli in accountsLo.DefaultIfEmpty()

                   select new MyDetail(det)
                   {
                       AccName = subAcc.name, 
                       AccRef = subAcc.accountnumber,
                       IncidentTicketNumber = subInc.ticketnumber,
                       IncidentKeyDescription = subInc.title,
                       IncidentMainContact = subInc.maincontactname
                    }).ToArray();


var groupedDetails = detailsOnly.GroupBy(c => c.det.detOtherId);

var query = db.headers.Where(head => head.IsDone == isdone
                                     && head.type == typeId
                                     && head.accountid == AccountId);

var result = query.Join(groupedDetails, c => c.headOtherId, c => c.Key, (a, b) => new MyHeader(a) { MyDetails = b })).ToList();

我可能会离开,但你看到了要点。我不认为它是最好的方式,但我没有任何LINQ-to-CRM经验,所以我不知道。但是这样的事情应该有用。有总比没有好。可能还存在语法错误,因为这还没有经过测试。