I have this code:
List<int> cats = new List<int>();
foreach (var cat in ctx.TblCategories)
{
int catCount = ctx.TblProducts
.Where(x => int.Parse( JsonConvert.DeserializeObject<SellItem>( x.ItemJson ).category ) == cat.Id )
.ToList()
.Count; <-- errors here
cats.Add(catCount);
}
//
public class SellItem
{
public string title { get; set; }
public string description { get; set; }
public string specification { get; set; }
public string additional { get; set; }
public string colour { get; set; }
public string condition { get; set; }
public string gender { get; set; }
public string size { get; set; }
public string category { get; set; }
public string postage { get; set; }
public string price { get; set; }
public string quantity { get; set; }
public bool active { get; set; }
public List<string> images { get; set; }
public List<string> imagenames { get; set; }
}
But i get an exception:
The LINQ expression
DbSet<TblProduct>().Where(t => int.Parse(JsonConvert.DeserializeObject<SellItem>(t.ItemJson).category) == __cat_Id_0)could not be translated.Additional information: Translation of method
Newtonsoft.Json.JsonConvert.DeserializeObjectfailed. If this method can be mapped to your custom function, see https://go.microsoft.com/fwlink/?linkid=2132413 for more information. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call toAsEnumerable,AsAsyncEnumerable,ToList, orToListAsync.See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
How do i resolve this?
List<int> cats = new List<int>();is where I stopped reading. A list of numbers is notcats. MaybecatIds. If you can't name things correctly, it's likely that you haven't understood them.JSON_QUERYetc functions (so no Linq) - or even worse: loading the entire table of JSON strings into memory so you can then deserialize it all. There's no redemption here: nuke it and start over.