rivantsov / vita

VITA Application Framework
MIT License
59 stars 15 forks source link

Queryable/Enumerable method MaxBy not supported. #208

Closed jasonlaw closed 2 years ago

jasonlaw commented 2 years ago

Error:

Vita.Data.Linq.Translation.LinqTranslationException
  HResult=0x80131500
  Message=Linq to SQL translation failed, invalid expression: Queryable/Enumerable method MaxBy not supported.
  Source=Vita
  StackTrace:
   at Vita.Data.Linq.LinqEngine.Translate(LinqCommand command)
   at Vita.Data.Sql.SqlFactory.GetLinqSql(LinqCommand command)
   at Vita.Data.Runtime.Database.ExecuteLinqSelect(EntitySession session, LinqCommand command, DataConnection conn)
   at Vita.Data.Runtime.Database.ExecuteLinqCommand(EntitySession session, LinqCommand command)
   at Vita.Data.Runtime.DataSource.ExecuteLinqCommand(EntitySession session, LinqCommand command)
   at Vita.Entities.Runtime.EntitySession.ExecuteLinqCommand(LinqCommand command, Boolean withIncludes)
   at Vita.Entities.Runtime.EntitySession.ExecuteQuery(Expression expression)
   at Vita.Entities.Runtime.EntitySession.ExecuteQuery[TResult](Expression expression)
   at Vita.Data.Linq.EntityQueryProvider.System.Linq.IQueryProvider.Execute[TResult](Expression expression)
   at Vita.Entities.Model.EntityQuery`1.System.Collections.Generic.IEnumerable<TEntity>.GetEnumerator()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at VIQCore.Community.BillingController.HealthCheck(String communityId) in C:\JSL\VIQCore\2021Net\VIQCommunityNET\VIQCore.Community\Controllers\BillingController.cs:line 202
   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync()
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync()

  This exception was originally thrown at this call stack:
    [External Code]

Inner Exception 1:
Exception: Queryable/Enumerable method MaxBy not supported.

My Code:

 var trans = session.EntitySet<IBillingAccount>()
                               .Select(x => x.Transactions.MaxBy(tr => tr.Id)).ToList();

            foreach (var tr in trans)
            {
                if ( tr.Balance != tr.Unit.OutstandingBalance)
                {
                    results.TotalCount += 1;
                    results.Results.Add(new BillingHealthCheck
                    {
                        CommunityId = tr.Account.Id,
                        UnitId = tr.Unit.Id,
                        LastTransBalance = tr.Balance,
                        OutStandingBalance = tr.Unit.OutstandingBalance
                    });
                }
            }                 
rivantsov commented 2 years ago

oh c'mon man, this method appeared in .NET 6, it's not even there in .Net 5: https://docs.microsoft.com/en-us/dotnet/api/system.linq.queryable.maxby?view=net-6.0&viewFallbackFrom=net-5.0

I believe you can rewrite your query, MaxBy is a combination of GroupBy and Max

jasonlaw commented 2 years ago

Opps..sorry, my bad, didn't aware of that. Will try on the GroupBy and Max as per suggested. Thank you very much and very sorry about it.