LINQ检查组中的所有记录

发布于 2024-12-08 05:26:38 字数 1449 浏览 0 评论 0原文

我正在从 linqQuery2 查询 linqQuery,因为 LINQ to CRM 不支持 GroupBy。下面的代码有效,但它只检查找到的第一条记录。是否有类似 First() 的东西,但检查所有记录而不仅仅是第一个记录?

var linqQuery = (from r in orgServiceContext.CreateQuery("opportunity")
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(),
        EmailedToRSM = !r.Contains("new_emailedtorsm") ? false : ((Boolean)r["new_emailedtorsm"]),
        LeadStatus = !r.Contains("new_leadstatus") ? "100000000" : ((OptionSetValue)r["new_leadstatus"]).Value.ToString(),
    });

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

foreach (var c in linqQuery2)
{

        if (c.LeadStatus.ToString() == "100000000")
            {

            //Count records that have a Lead Status of 100000000

            }
}

谢谢!

I am querying linqQuery from linqQuery2 because LINQ to CRM doesn't support GroupBy. The code below works, but it only checks against the first record found. Is there something like First() but checks all the records and not just the First one?

var linqQuery = (from r in orgServiceContext.CreateQuery("opportunity")
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(),
        EmailedToRSM = !r.Contains("new_emailedtorsm") ? false : ((Boolean)r["new_emailedtorsm"]),
        LeadStatus = !r.Contains("new_leadstatus") ? "100000000" : ((OptionSetValue)r["new_leadstatus"]).Value.ToString(),
    });

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

foreach (var c in linqQuery2)
{

        if (c.LeadStatus.ToString() == "100000000")
            {

            //Count records that have a Lead Status of 100000000

            }
}

Thanks!

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

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

发布评论

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

评论(2

离笑几人歌 2024-12-15 05:26:38

听起来您想使用 myGroup 中的所有项目。通过调用 First(),您只能将自己限制为第一项。

您可以按如下方式编写查询:

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

但是,请注意,这样做会使结果变平。

It sounds like you want to use all the items in myGroup. By calling First() you're limiting yourself to the first item only.

You could write your query as follows:

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

However, note that by doing so you would be flattening the results.

镜花水月 2024-12-15 05:26:38

目前尚不清楚为什么要使用 First() 开始。怎么样:

var grouped = linqQuery.GroupBy(f => f.OwnerId).ToList();

现在“分组”中的每个条目都是一个分组。您现在可以使用:

foreach (var group in grouped)
{
    Console.WriteLine("OwnerId: {0}", group.Key);
    int leadCount = group.Count(c => c.LeadStatus == 100000000);
    // etc - do what whatever else you want with the group

    // Let's dump all the customer IDs...
    foreach (var entry in group)
    {
        Console.WriteLine(entry.CustomerID);
    }
}

It's not clear why you're using First() to start with. How about:

var grouped = linqQuery.GroupBy(f => f.OwnerId).ToList();

Now each entry in "grouped" is a grouping. You can now use:

foreach (var group in grouped)
{
    Console.WriteLine("OwnerId: {0}", group.Key);
    int leadCount = group.Count(c => c.LeadStatus == 100000000);
    // etc - do what whatever else you want with the group

    // Let's dump all the customer IDs...
    foreach (var entry in group)
    {
        Console.WriteLine(entry.CustomerID);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文