在 LINQ 查询中使用组
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设组中所有项目的 OwnerNames 都是相同的,那么您可以执行以下操作:
编辑:
如果您想多次使用第一个项目,我建议您使用“let”操作员:
Assuming the OwnerNames will be identical for all the items in the group then you could do something like:
Edit:
If you want to use the first item multiple times, I'd suggest you use the 'let' operator:
交替尝试
:
Try
Alternately: