在 LINQ 查询中使用组

发布于 2024-12-08 02:18:26 字数 1816 浏览 0 评论 0原文

我正在使用 LINQ to CRM 提供商。

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

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 查询 linqQuery2 中的其他变量。

所以我想要这样的东西:

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

这可能吗?

I am using the LINQ to CRM provider.

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()
                  });

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

So I would want something like:

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

Is this possible to do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌辣 2024-12-15 02:18:26

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

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

编辑:

如果您想多次使用第一个项目,我建议您使用“let”操作员:

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()
                    });

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()
    });

Edit:

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()
                    });
迷雾森÷林ヴ 2024-12-15 02:18:26

交替尝试

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

        var linqQuery2 = linqQuery.ToList()
            .GroupBy(f => f.OwnerName)
            .Select(myGroup => new
                                {
                                    OwnerName = myGroup.Key,
                                    OwnerCount = myGroup.Count()
                                });

Try

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

Alternately:

        var linqQuery2 = linqQuery.ToList()
            .GroupBy(f => f.OwnerName)
            .Select(myGroup => new
                                {
                                    OwnerName = myGroup.Key,
                                    OwnerCount = myGroup.Count()
                                });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文