After upgrading from .Net2.2 to .Net7, the following LINQ expression fails with this error "LINQ expression could not not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly":
string excludeString = "XX";
string excludeString2 = "XX";
var groupfilter = await _db.UserGroup.Where(q => q.UserName == currUserName && q.IsActive == false && q.WorkGroupName == "BB").ToListAsync();
if (groupfilter.Any())
excludeString = "BB";
groupfilter = await _db.UserGroup.Where(q => q.UserName == currUserName && q.IsActive == false && q.WorkGroupName == "TS").ToListAsync();
if (groupfilter.Any())
excludeString2 = "TS";
DriveListViewModel model = new DriveListViewModel()
{
Drive = await _db.Drive
.Where(m => m.StatusId == 5 || m.StatusId == 1010 || m.StatusId == 1012)
.Where(m => m.LoadingComplete == null)
.Where(m => !m.UnitCode.Contains(excludeString))
.Where(m => !m.UnitCode.Contains(excludeString2))
.Include(s => s.DriveStatus)
.Include(d => d.Location)
.Include(f => f.Item)
.GroupBy(m => m.RegistrationNumber)
.Select(m => m.FirstOrDefault())
.OrderBy(m => m.DriverToLoad)
.ToListAsync(),
machineryList = await _db.Machinery.ToListAsync(),
cleaningList = await _db.Cleaning.ToListAsync(),
};
Disabling these 3 lines removes the error:
.GroupBy(m => m.RegistrationNumber)
.Select(m => m.FirstOrDefault())
.OrderBy(m => m.DriverToLoad)
...however, I need this list to be distinct on "RegistrationNumber", so I need a modified query to obtain the same result.
Any ideas how to solve that in .Net7 ?