如何在IQueryable.Select方法中声明变量?

时间:2017-04-30 05:59:44

标签: linq linq-to-entities

我的问题是,在此行剩余=金额 - 成本, 有没有办法在Select中声明一个变量,我可以在linq select中重用?我不想调用_myContext.BudgetDocItems.Where(z =&gt; z.SL.TLRef == x.SL.TLRef)。选择(y =&gt; y.Amount).Sum()两次。< / p>

startScreen.mcOptions.addEventListener(MouseEvent.CLICK, onTap);

private function onTap(e:MouseEvent):void 
    {
        if (e.currentTarget == startScreen.mcOptions.mcPlay )
        {
            trace("PLAY OPTION TRUE");
        }else
        if (e.currentTarget == startScreen.mcOptions.mcStore )
        {
            trace("PLAY OPTION TRUE");
        }else
    }

1 个答案:

答案 0 :(得分:2)

首先选择值,然后使用它们:

var test = myContext.BudgetDocItems

.Select(x => new
    {
        Amount = myContext.BudgetDocItems.Where(z => z.SL.TLRef == x.SL.TLRef).Sum(y => y.Amount),
        Cost = myContext.AccDocItemViews.Where(z => z.SLRef == x.SLRef).Sum(y => y.Debit)
    })

.Select(x => new BudgetDocItem1
    {
        Amount = x.Amount,
        Cost = x.Cost,
        Remaining = x.Amount - x.Cost
    })
.ToList();