这是我用过的 LINQ 查询
var result = (from price in inventoryDb.Pricing.AsNoTracking()
where price.Quantity > 0m
select new
{
TagNo = price.TagNo,
SellingRate = price.SellingRate,
Quantity = price.Quantity
}).ToList();
基于Quantity
value 我需要在列表中生成重复的项目。
Output :
result = [0]{TagNo="100", SellingRate=1500.00, Quantity=1}
[1]{TagNo="101", SellingRate=1600.00, Quantity=2}
预期结果:
result = [0]{TagNo="100", SellingRate=1500.00}
[1]{TagNo="101", SellingRate=1600.00}
[2]{TagNo="101", SellingRate=1600.00}
您可以使用Enumerable.SelectMany
+ Enumerable.Range
:
var result = inventoryDb.Pricing.AsNoTracking()
.Where(p => p.Quantity > 0m)
.SelectMany(p => Enumerable.Range(0, p.Quantity)
.Select(i => new
{
TagNo = p.TagNo,
SellingRate = p.SellingRate
}))
.ToList();
如果您的 LINQ 提供商不支持(例如Linq-To-Entities
),最简单的是使用Linq-To-Objects
。为了避免所有内容都加载到内存中,您应该使用AsEnumerable
之后Where
:
var result = inventoryDb.Pricing.AsNoTracking()
.Where(p => p.Quantity > 0m)
.AsEnumerable()
.SelectMany(p => Enumerable.Range(0, p.Quantity)
.Select(i => new
{
TagNo = p.TagNo,
SellingRate = p.SellingRate
}))
.ToList();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)