Skip to main content

Timeline for answer to LINQ Expression for remove duplicates by ocuenca

Current License: CC BY-SA 4.0

Post Revisions

22 events
when toggle format what by license comment
Feb 10, 2020 at 20:56 comment added Konrad Dybkowski I have one more issue in other method. You say me to leave DateTime but in this method: pastebin.com/LAeYefTE when I compare string orderPlaced and string x.OrderDetails.OrderPlaced the query works. Now in database i have DateTime2 type and in parameter of this method DateTime. Is there way to check if this 2 dates are equal? What should I do?
Feb 10, 2020 at 20:20 comment added ocuenca A post with the same issue than you, and yes, I guess the solution for now is going to be calling AsEnumerable before grouping. Maybe someone else knows a better solution
Feb 10, 2020 at 20:12 comment added ocuenca EF 3 made heavy changes for client evaluations, but I don't know why your Group can't be translated, I'm not seeing nothing special in the group, for now move the AsEnumerable before the grouping, trying to see why can't be translated to sql, learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/…
Feb 10, 2020 at 20:04 comment added Konrad Dybkowski Query: pastebin.com/PsD0CEP3 Controller: pastebin.com/G3EDhY75 OrderDTO: pastebin.com/FTUtJKfv GetOrdersView: pastebin.com/vEe6Mt6v
Feb 10, 2020 at 20:01 comment added Konrad Dybkowski EF Core 3.1 / Microsoft.EntityFrameworkCore.SqlServer 3.1.1 When I use to List(): System.InvalidOperationException: 'Client side GroupBy is not supported.'
Feb 10, 2020 at 19:54 comment added ocuenca And what happened after calling ToList()?, .AsEnumerable is going to avoid any problem you could have in the Select, which I don't understand why is not translating your query, now the filtering and the grouping should be in the DB side and the projection in the client side, I'm running out of ideas, What version of EF are you using and for what DB provider?
Feb 10, 2020 at 19:47 comment added Konrad Dybkowski pastebin.com/L6pfH195 Can you fix this? I tried ToList also.
Feb 10, 2020 at 19:42 history edited ocuenca CC BY-SA 4.0
added 40 characters in body
Feb 10, 2020 at 19:42 comment added ocuenca You need to call ToList before going to the view, that will force to execute the query before going to the view
Feb 10, 2020 at 19:41 comment added Konrad Dybkowski InvalidOperationException: Client side GroupBy is not supported. AspNetCore.Views_Order_GetOrders.ExecuteAsync() in GetOrders.cshtml + 13 @foreach (var item in Model)
Feb 10, 2020 at 19:34 comment added ocuenca Just to know if it's the OrderBy, call .AsEnumerable() before calling the Select
Feb 10, 2020 at 19:32 comment added ocuenca I strongly recommend you to use DateTime instead of string in case it's in your hands to change the schema.
Feb 10, 2020 at 19:31 comment added Konrad Dybkowski With FirstOrDefault: InvalidOperationException: The LINQ expression '(GroupByShaperExpression: KeySelector: (o.OrderPlaced), ElementSelector:(EntityShaperExpression: EntityType: Order ValueBufferExpression: (ProjectionBindingExpression: EmptyProjectionMember) IsNullable: False ) ) .OrderBy(e => e.OrderId)' 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 either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See go.microsoft.com/fwlink/?linkid=2101038 for more info.
Feb 10, 2020 at 19:29 comment added Konrad Dybkowski It is a string.
Feb 10, 2020 at 19:28 comment added ocuenca Is OrderPlaced a DateTime? Use also FirstOrDefault instead or First
Feb 10, 2020 at 19:26 history edited ocuenca CC BY-SA 4.0
added 9 characters in body
Feb 10, 2020 at 19:25 comment added Konrad Dybkowski Error Message: InvalidOperationException: The LINQ expression '(GroupByShaperExpression: KeySelector: (o.OrderPlaced), ElementSelector:(EntityShaperExpression: EntityType: Order ValueBufferExpression: (ProjectionBindingExpression: EmptyProjectionMember) IsNullable: False ) ) .OrderBy(e => e.OrderId)' 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 either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See go.microsoft.com/fwlink/?linkid=2101038 for more information.
Feb 10, 2020 at 19:24 history edited ocuenca CC BY-SA 4.0
added 168 characters in body
Feb 10, 2020 at 19:23 comment added Konrad Dybkowski OrderDTO: ` public int Id { get; set; } public string OrderPlaced { get; set; } public decimal Total { get; set; }`
Feb 10, 2020 at 19:23 comment added Konrad Dybkowski It doesn't work :( var query = _appDbContext.Orders.Include(x => x.OrderDetails) .Where(x => x.OrderDetails.UserId == userId) .GroupBy(x => x.OrderDetails.OrderPlaced) .Select(g => new OrderDTO { Id = g.OrderBy(e => e.OrderId).First().OrderId, OrderPlaced = g.Key, Total = g.Sum(e => e.Price * e.Amount), } );
Feb 10, 2020 at 19:19 history edited ocuenca CC BY-SA 4.0
edited body
Feb 10, 2020 at 19:08 history answered ocuenca CC BY-SA 4.0