我从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
}
}
谢谢!
答案 0 :(得分:2)
听起来你想要使用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()
};
但是,请注意,这样做会使结果变平。
答案 1 :(得分:1)
目前尚不清楚为什么要使用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);
}
}