noetico
noetico

Reputation: 35

Lambda expression with statement body error in previously working code

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

Upvotes: 0

Views: 3203

Answers (2)

Yosef Bernal
Yosef Bernal

Reputation: 1064

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

Upvotes: 3

Mattias Santoro
Mattias Santoro

Reputation: 184

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

Upvotes: 1

Related Questions