[英]Dynamic values in linq query by other field and boolean value
var results =
(from p in DBContext.Sources
orderby p.AccountTypeId
group p by new { p.AccountTypeId } into g
select new ObjectItem
{
AccountTypeId = g.Key.AccountTypeId,
Packages =
(from pkg in g
group pkg by new
{
pkg.PackageId,
pkg.Enabled
} into pg
select new ATSourcePackageItem
{
PackageId = pg.Key.PackageId,
DisplayName = pg.Key.DisplayName,
CategoryId = pg.Key.DisplayCategoryId,
Prices =
(from pr in pg
group pr by new { pr.Fpt, pr.Price } into prg
select new ATSourcePriceItem
{
FPT = prg.Key.Fpt,
Amount = prg.Key.Price
})
})
}).ToList();
I want to subtract Amount
by 10 when CategoryId
equals 7 and when a condition is true.当CategoryId
等于 7 且条件为真时,我想将Amount
减去 10。
Is it possible to somehow do this within this linq query because the Amount object doesn't have a setter and this is the only place how I can edit the value.是否可以在此 linq 查询中以某种方式执行此操作,因为 Amount 对象没有设置器,这是我编辑值的唯一方法。
For example if I do something like:例如,如果我执行以下操作:
Amount = prg.Key.Price - 10
I'm able to get the value I want, but I want also to set that for specific category and if the boolean value is true.我能够获得我想要的值,但我还想为特定类别设置该值,并且布尔值是否为真。
If you can do this:如果你可以这样做:
Amount = prg.Key.Price - 10
You can also do this:你也可以这样做:
Amount = {bool expression} ? prg.Key.Price - 10 : prg.Key.Price
or just要不就
Amount = prg.Key.Price - ({bool expression} ? 10 : 0)
You can use a ternary operator here :您可以在此处使用三元运算符:
//Amount = prg.Key.Price
Amount = (pg.Key.DisplayCategoryId == 7 && your_boolean_here) ?
prg.Key.Price - 10 : prg.Key.Price
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.