在LINQ查询中使用组 [英] Using Group in LINQ Query

查看:131
本文介绍了在LINQ查询中使用组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用LINQ to CRM提供程序.

I am using the LINQ to CRM provider.

我正在查询信息,然后使用LINQ来查询LINQ to CRM查询,因此我可以使用GroupBy,因为LINQ to CRM提供程序不支持它.这就是我到目前为止所拥有的.

I am querying the information then I am using LINQ to query the LINQ to CRM query so I can use GroupBy, since the LINQ to CRM provider does not support it. This is what I have so far.

var linqQuery = (from r in orgServiceContext.CreateQuery("opportunity")
    join c in orgServiceContext.CreateQuery("contact") on 
        ((EntityReference)r["new_contact"]).Id equals c["contactid"] into opp
    from o in opp.DefaultIfEmpty()
    select new
    {
        OpportunityId = !r.Contains("opportunityid") 
            ? string.Empty : r["opportunityid"],
        CustomerId = !r.Contains("customerid") 
            ? string.Empty : ((EntityReference)r["customerid"]).Name,
        OwnerId = !r.Contains("ownerid") 
            ? string.Empty : ((EntityReference)r["ownerid"]).Id.ToString(),
        OwnerName = !r.Contains("ownerid") 
            ? string.Empty : ((EntityReference)r["ownerid"]).Name.ToString(),
        Priority = !r.Contains("opportunityratingcode") 
            ? string.Empty : r.FormattedValues["opportunityratingcode"],
        ContactName = !r.Contains("new_contact") 
            ? string.Empty : ((EntityReference)r["new_contact"]).Name,
        // ...
        // other properties  
    });

var linqQuery2 = (from f in linqQuery.ToList()
                  group f by f.OwnerId into myGroup
                  select new
                  {
                      OrderCount = myGroup.Count()
                  });

但是我还希望能够从linqQuery2查询linqQuery中的其他变量.

But I also want to be able to query the other variables that are in linqQuery from linqQuery2.

所以我想要类似的东西

var linqQuery2 = (from f in linqQuery.ToList()
                  group f by f.OwnerId into myGroup
                  select new
                  {
                      OwnerName = myGroup.OwnerName,
                      OrderCount = myGroup.Count()
                  });

这可能吗?

推荐答案

假设该组中所有项目的OwnerNames都相同,那么您可以执行以下操作:

Assuming the OwnerNames will be identical for all the items in the group then you could do something like:

var linqQuery2 = (from f in linqQuery.ToList()
    group f by f.OwnerId into myGroup
    select new
    {
        OwnerName = myGroup.First().OwnerName,
        OrderCount = myGroup.Count()
    });

如果您想多次使用第一个项目,建议您使用'let'运算符:

If you want to use the first item multiple times, I'd suggest you use the 'let' operator:

var linqQuery2 = (from f in linqQuery.ToList()
                    group f by f.OwnerId into myGroup
                    let first = myGroup.First()
                    select new
                    {
                        OwnerName = first.OwnerName,
                        OrderCount = myGroup.Count()
                    });

这篇关于在LINQ查询中使用组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆