其他字段和布尔值的 linq 查询中的动态值

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();

CategoryId 等于 7 且条件为真时,我想将 Amount 减去 10。

是否可以在此 linq 查询中以某种方式执行此操作,因为 Amount 对象没有 setter,这是我唯一可以编辑该值的地方。

例如,如果我这样做:

Amount = prg.Key.Price - 10

我能够得到我想要的值,但我还想为特定类别设置它,如果布尔值为真。

如果你能做到:

Amount = prg.Key.Price - 10

您也可以这样做:

Amount = {bool expression} ? prg.Key.Price - 10 : prg.Key.Price

或者只是

Amount = prg.Key.Price - ({bool expression} ? 10 : 0)

您可以在此处使用三元运算符:

//Amount = prg.Key.Price
Amount = (pg.Key.DisplayCategoryId == 7 && your_boolean_here) ? 
    prg.Key.Price - 10 : prg.Key.Price