C
C#3y ago
nlohim

✅ LINQ GroupBy error

I don't understand what am I missing when trying to perform a simple GroupBy?
18 Replies
Amos
Amos3y ago
@talje since this is a database call use await. Using .Where() here also var query = await context.DosimeterAssocation.Where(x=>x.Barcode).ToListAsync();
nlohim
nlohimOP3y ago
@Ayymoss this is GroupBy not Where
Amos
Amos3y ago
Certain database provides will be able to translate that GroupBy LINQ method. The one you're using may not work.
nlohim
nlohimOP3y ago
I'm using MSSQL
Amos
Amos3y ago
I'll check mine and see if that LINQ works with PostgreSQL GroupBy didn't work for me either You could get the data you need then do it in memory
Amos
Amos3y ago
@talje
nlohim
nlohimOP3y ago
I tried awaiting, didn't work
Amos
Amos3y ago
Is your method async?
nlohim
nlohimOP3y ago
Yeah ofc, it wouldn't compile otherwise
Amos
Amos3y ago
I ask cuz "didn't work" isn't very helpful 😄 But yeah. I'd suggest doing filtering in database query then groupby in memory if you need everything, then in my screenshot that shows that
nlohim
nlohimOP3y ago
I have tried that by calling ToList() before GroupBy(), the result is the same error
Amos
Amos3y ago
Try following what I did using await and async methods You don't really want to mix them since .ToList is not asynchronous
Angius
Angius3y ago
test is no longer in the database tho, it's just a regular list Nothing async to be done there
Amos
Amos3y ago
Ya - the EFCore query is async though, second is in memory grouping
Angius
Angius3y ago
On the topic, though: .GroupBy() can't really be translated to LINQ on its own. You'd need to pair it with .Select() and grab just the keys or some of the properties of those keys or some such
Amos
Amos3y ago
I have tried that by calling ToList() before GroupBy(), the result is the same error
I think they're talking about doing .ToList() on the DbSet then .GroupBy() TIL
Accord
Accord3y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
nlohim
nlohimOP3y ago
/close

Did you find this page helpful?