I'm very new to LINQ and one of my first attempts at using it had to be super complicated!
I am trying to adapt the accepted answer in this Stack Over question.
I have written it thus:
using (var ctx = new MyEntities())
{
var q = ctx.tblPrices.OrderByDescending(x => x.Cost)
.GroupBy(x => x.ItemID)
.Select(g => new {g, count = g.Count()})
.SelectMany(t => t.g.Select(b => b).Zip(Enumerable.Range(1, t.count), (j, i) => new {j.WebPrice, j.PriceID, j.ItemID}));
foreach (var i in q)
{
sb.AppendFormat("Id = {0}, Name = {1}, ItemId = {2}<hr/>", i.WebPrice, i.PriceID, i.ItemID);
}
}
Basically, I want to return the cheapest price for the same items from different suppliers.
However, when I run the project I get the following very long error message and I have no idea what it means. But it looks pretty serious!
LINQ to Entities does not recognize the method 'System.Collections.Generic.IEnumerable
1[<>f__AnonymousType53[System.Nullable1[System.Decimal], System.Int32,System.Int32]] Zip[tblPrice,Int32,<>f__AnonymousType53](System.Collections.Generic.IEnumerable1[MyProjectMVC.Models.tblPrice], System.Collections.Generic.IEnumerable1[System.Int32], System.Func3[MyProjectMVC.Models.tblPrice,System.Int32, <>f__AnonymousType53[System.Nullable`1[System.Decimal],System.Int32,System.Int32]])' method, and this method cannot be translated into a store expression.
Any help would be greatly appreciated.
method cannot be translated into a store expression.Linq to SQL isn't able to translate part of your query into an SQL command. You may have to get the results from the database first (a simple.ToListusually does the trick) and then process them.ItemID, along with theWebPriceandPriceIDrepresenting the cheapest price?var q = ctx.tblPrices.OrderBy(x => x.ItemID).ThenByDescending(x => x.Cost);tell you what you need?