question

MiguelLOKOSS-0322 avatar image
0 Votes"
MiguelLOKOSS-0322 asked MiguelLOKOSS-0322 commented

Resolve Exception FirstOrDefault could not be translated (Net Core5)

Hello everyone, I took over an old project that was working great that I updated to .net Core 5
I suddenly have this exception when executing my queries. Despite several searches on the subject, I still cannot find any useful solutions. Thanks in advance to anyone who could guide me on a track

Exception: .FirstOrDefault()' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'

 //List
     filteredSoldRetailsDtoList = param.sale_product_retails
                                                 .Where(x => x.vente.agency.id_enterprise == id_enterprise
                                                && x.sale.date_sale > startDate && x.sale.date_sale < endDate); 
    
  //Best productSale
                     var BestproductSale= filteredSoldRetailsDtoList 
                         .GroupBy(x => x.product_type_measure.id)
                         .Select(grp => new 
                                                  { number = grp.Key,
                                                    sum = grp.Sum(x => x.quantity * x.p_sale),
                                                    product = grp.FirstOrDefault().product_type_measure })
                         .OrderByDescending(x => x.sum).ToList();

...


dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

TimonYang-MSFT avatar image
0 Votes"
TimonYang-MSFT answered MiguelLOKOSS-0322 commented

This should be an EF Core issue.

Starting with EF Core 3.x, EF no longer evaluates LINQ queries on the client side. You can see this change in the documentation.

Breaking changes included in EF Core 3.x

The easiest way to modify should be to follow the prompts of the exception information, use AsEnumerable() or ToList() to transfer the query to the local instead of the database side.

             //Wrong way
             var BestproductSale = model.ScoreTables
                       .GroupBy(x => x.Math)
                       .Select(grp => new
                       {
                           number = grp.Key,
                           product = grp.ToList().FirstOrDefault().Name
                       })
                       .OrderByDescending(x => x.number).ToList();

             //Right way
             var BestproductSale = model.ScoreTables.ToList()
                       .GroupBy(x => x.Math)
                       .Select(grp => new
                       {
                           number = grp.Key,
                           product = grp.ToList().FirstOrDefault().Name
                       })
                       .OrderByDescending(x => x.number).ToList();

If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Problem solved. A little bit of ToList () actually solved the problem. A big Thank you to all !

0 Votes 0 ·

That ToList will probably transfer the whole table from database to your program. Make sure that the table is not too big.

0 Votes 0 ·
karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered

I don't know your data but perhaps you can something like the following.

127173-grouped.png



grouped.png (37.8 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.