admin管理员组文章数量:1122832
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.DeserializeObject
failed. If this method can be mapped to your custom function, see /?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 /?linkid=2101038 for more information.
How do i resolve this?
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.DeserializeObject
failed. 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?
Share Improve this question edited Nov 21, 2024 at 18:01 Dai 155k30 gold badges295 silver badges419 bronze badges asked Nov 21, 2024 at 17:35 redoc01redoc01 2,3035 gold badges35 silver badges68 bronze badges 7 | Show 2 more comments1 Answer
Reset to default 0As Neil says you can't deserialize data on the server side. And the solution is in your Additional information. You need to add .AsEnumerable to switch it to client-side evalutation.
You can try this:
List<int> cats = new List<int>();
foreach (var cat in ctx.TblCategories)
{
int catCount = ctx.TblProducts
.AsEnumerable() // Switch to client-side evaluation
.Where(x => int.Parse( JsonConvert.DeserializeObject<SellItem>( x.ItemJson ).category ) == cat.Id )
.Count;
cats.Add(catCount);
}
Hope it will help.
本文标签: cLinqHow to query json inside LinqStack Overflow
版权声明:本文标题:c# - Linq - How to query json inside Linq - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736308379a1933642.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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. – Thomas Weller Commented Nov 21, 2024 at 18:08JSON_QUERY
etc 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. – Dai Commented Nov 21, 2024 at 21:50