我有一个问题,它指的是是否可以访问LINQ中的另一个属性以在Linq中设置另一个属性的值:
var v = ctx.myTableItems
.Where(x => x.id== anotherid)
.Select(e => new MyViewModel
{
Title = e.Title,
CurrentPrice = e.CurrenctPrice.Value,
ItemID = e.ItemID.ToString(),
Transactions= e.Transactions,
Sales =
})
.Where(x=>x.Sales>0);
因此,请检查一下这个人,当我设置交易属性时,我将交易集合设置在上一行中,现在我想将我的销售属性设置为以前行中分配的交易属性:
Transactions= e.Transactions,
Sales = e.Transactions.Sum(x=>x.Quantity) // Normaly I'd do it like this
,但我想尝试这样的东西:
Sales = Transactions.Sum(x=>x.Quantity) // is this doable ?
所以我的问题是,是否可以在Select语句中使用以前属性的值设置另一个属性?就我而言,是交易集?
这是可行的,如果是的话,如何?
P.S。我试图弄清楚这是否可行,因为如果我可以使用此之前设置的属性值,那么我会避免在DB中的交易表上执行2个不确定的查询?
您无法使用上一个值,因为您不想为每个项目调用e.transactions.sum((。
只需将方法添加到MyViewModel
public double GetSales()
{
return Transactions.Sum(x=>x.Quantity);
}
//at the end of your code use:
.Where(x=>x.GetSales()>0);