var inventionIds = new List<int>();
List<ProductRemunerationReportingPeriodLink> productRemunerationReportingList =
GetAllProductRemunerationReportingPeriodByProductId(productId);
foreach (var rptPeriod in productRemunerationReportingList)
{
var inventionsList = rptPeriod.Inventions;
foreach (var link in inventionsList)
{
int id = link.Invention.InventionId;
inventionIds.Add(id);
}
}
return inventionIds;
我想最小化两个for循环任何一个请为此提供一个nhibernate linq查询。任何帮助都是非常明确的。
答案 0 :(得分:3)
使用原始变量名称:
var inventionIds =
productRemunerationReportingList.SelectMany(rptPeriod => rptPeriod.Inventions)
.Select(ProductRemunerationReportingPeriodInvention => ProductRemunerationReportingPeriodInvention.Invention.InventionId)
.ToList();
与更好的命名相同:
var inventionIds = GetAllProductRemunerationReportingPeriodByProductId(productId)
.SelectMany(p => p.Inventions)
.Select(i => i.Invention.InventionId)
.ToList();
提示:变量名的长度应对应于使用变量的范围。全局可访问的变量应具有良好的描述性名称。在像lambda这样的短范围内使用的变量应该有很短的名称。
答案 1 :(得分:0)
List<int> inventionIds = (from rptPeriod in productRemunerationReportingList
from ProductRemunerationReportingPeriodInvention in rptPeriod.Inventions
select ProductRemunerationReportingPeriodInvention.Invention.InventionId).ToList<int>();