LINQ检查组中的所有记录
我正在从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来您想使用
myGroup
中的所有项目。通过调用First()
,您只能将自己限制为第一项。您可以按如下方式编写查询:
但是,请注意,这样做会使结果变平。
It sounds like you want to use all the items in
myGroup
. By callingFirst()
you're limiting yourself to the first item only.You could write your query as follows:
However, note that by doing so you would be flattening the results.
目前尚不清楚为什么要使用
First()
开始。怎么样:现在“分组”中的每个条目都是一个分组。您现在可以使用:
It's not clear why you're using
First()
to start with. How about:Now each entry in "grouped" is a grouping. You can now use: