Lambda expression with statement body error in previously working code

£可爱£侵袭症+ 提交于 2021-01-29 11:27:24

问题


I need help with this, I have a very important piece of code that I got help with from this thread

Here

The code is as follows:

var pln = db.tabStockPlanners
            .Where(y => y.ExpectedHarvestDate < addoneyear)
            .Where(r => r.Published == 1)
            .Where(f => f.Available == 1);

var gruppedList = pln.GroupBy(x => x.ItemID, (key, enumerable) =>
        {
            return new tabStockPlanner { ItemID = key, ExpectedYieldInTonnes = enumerable.Sum(k => k.ExpectedYieldInTonnes) };
        }).OrderByDescending(t => t.ExpectedYieldInTonnes).ToList();

But I get an error

A lambda expression with a statement body cannot be converted to an expression tree

I do not know if this is something that has changed with EF. Can someone help me with this? EF is version 6. Any issues with the current code?

UPDATE

What has worked perfectly is:

 var gplist = from x in db.tabStockPlanners
                     where x.Published == 1
                     where x.Available == 1
                     where x.ExpectedHarvestDate < addoneyear
                     group x.ExpectedYieldInTonnes by new { x.ItemID }
                     into g
                     select new { g.Key.ItemID, ExpectedYieldInTonnes = g.Sum() };

But I would like to also have the correct answer as per this syntax. Maybe someone can repost with the LINQ syntax. Thank you.

UPDATE Hello Friends; @YosefBinmal solution worked well, along with @NetMage proposal for AsEnumerable() I am posting Yosef's solution with modifications to make it anonymous and return just the 2 columns needed. I removed the tabStockPlanner so its new { and it is solid. So marking that as correct. Any objections? It works well.

Here's the modified code

  var pln = db.tabStockPlanners
          .Where(y => y.ExpectedHarvestDate < addoneyear)
          .Where(r => r.Published == 1)
          .Where(f => f.Available == 1);

        var gruppedList = pln
            .AsEnumerable()
.GroupBy(i => i.ItemID)
.Select(g => new { ItemID = g.Key, ExpectedYieldInTonnes = g.Sum(i => i.ExpectedYieldInTonnes) })
.OrderByDescending(t => t.ExpectedYieldInTonnes)
.ToList();

回答1:


Amend: The GroupBy extension method you are using takes an expression lambda as it's second parameter. Your lambda contains a { return ... } statement that can't be translated into an Expression tree. Please see CS0834.

You could avoid this error by writing:

var gruppedList = pln
    .GroupBy(i => i.ItemID) 
    .Select(g => new tabStockPlanner { ItemID = g.key, ExpectedYieldInTonnes = g.Sum(i => i.ExpectedYieldInTonnes) })
    .OrderByDescending(t => t.ExpectedYieldInTonnes)
    .ToList();

I like to use the overload of GroupBy that only takes keySelector (without the element selector). This way the LINQ pipeline becomes more readable in my opinion as each function has a single task.

Hope it helps you




回答2:


Replace with

var gruppedList = pln.GroupBy(x => x.ItemID, (key, enumerable) => new tabStockPlanner { ItemID = key, ExpectedYieldInTonnes = enumerable.Sum(k => k.ExpectedYieldInTonnes) })
.OrderByDescending(t => t.ExpectedYieldInTonnes).ToList();


来源:https://stackoverflow.com/questions/59491014/lambda-expression-with-statement-body-error-in-previously-working-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!