Resolve Exception FirstOrDefault could not be translated (Net Core5)

Miguel LOKOSS 21 Reputation points
2021-08-27T11:45:28.713+00:00

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();

...

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
0 comments No comments
{count} votes

Accepted answer
  1. Timon Yang-MSFT 9,586 Reputation points
    2021-08-30T02:33:11.007+00:00

    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.


1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,386 Reputation points
    2021-08-27T14:10:52.073+00:00

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

    127173-grouped.png

    0 comments No comments