Ash*_*ain 4 linq .net-4.0 linq-to-sql
我正在写两个表的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);
}
}
Run Code Online (Sandbox Code Playgroud)
一个问题是你无缘无故地使用了查询延续.请注意,这仍然不应该阻止您使用该Key属性.试试这个稍微清洁的方法:
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 { bp.ProductId,
bp.ProductName,
bpf.ProductMasterName};
foreach (var group in products)
{
var key = group.Key;
// Can now use key.ProductName, key.ProductMasterName etc.
}
Run Code Online (Sandbox Code Playgroud)
至于你将prodName变量设置为什么- 目前还不清楚你想要什么.第一个ProductName值?最后?所有这些的串联?为什么你需要分组呢?
| 归档时间: |
|
| 查看次数: |
5122 次 |
| 最近记录: |