在匿名类型上编写 Group By
我正在两个表上编写一个 group by 子句,这两个表已连接并通过实体数据模型进行访问。我无法迭代匿名类型,有人可以帮助我吗?
public string GetProductNameByProductId(int productId)
{
string prodName=string.Empty;
using (VODConnection vodObjectContext = new VODConnection())
{
var products = from bp in vodObjectContext.BFProducts
join bpf in vodObjectContext.BFProductMasters on bp.ProductMasterId equals bpf.ProductMasterId
where bp.ProductId == productId
group bp by new { ProductId = bp.ProductId, ProductName = bp.ProductName, ProductMasterName=bpf.ProductMasterName} into newInfo
select newInfo;
//Want to iterate over products or in fact need to get all the results. How can I do that? Want productmastername property to be set in prodName variable by iterating
return (prodName);
}
}
I am writting a group by clause on two tables which are joined and being accessed via Entity Data Model. I am not able to iterate over the anonymous type, can somebody help me out.
public string GetProductNameByProductId(int productId)
{
string prodName=string.Empty;
using (VODConnection vodObjectContext = new VODConnection())
{
var products = from bp in vodObjectContext.BFProducts
join bpf in vodObjectContext.BFProductMasters on bp.ProductMasterId equals bpf.ProductMasterId
where bp.ProductId == productId
group bp by new { ProductId = bp.ProductId, ProductName = bp.ProductName, ProductMasterName=bpf.ProductMasterName} into newInfo
select newInfo;
//Want to iterate over products or in fact need to get all the results. How can I do that? Want productmastername property to be set in prodName variable by iterating
return (prodName);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个问题是您无缘无故地使用了查询延续。请注意,这仍然不应该阻止您使用
Key
属性。尝试使用一种稍微简洁的方法:至于将
prodName
变量设置为什么 - 目前还不清楚您到底想要什么。第一个ProductName
值?最后一个?所有这些的串联?为什么需要分组?One problem is that you've used a query continuation for no reason. That still shouldn't have prevented you from using the
Key
property, mind you. Try this as a slightly cleaner approach:As for what you set your
prodName
variable to - it's unclear exactly what you want. The firstProductName
value? The last? A concatenation of all of them? Why do you need a grouping at all?