开发者

LINQ Checking all Records in Group

开发者 https://www.devze.com 2023-04-11 04:24 出处:网络
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 check

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!


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消