Home > database >  The long expression could not be translated
The long expression could not be translated

Time:05-25

I am trying to execute this query


    var userAndLovedOnesQuery = await (from uq in _context.Users
    
                                                           where uq.SubjectId == request.SubjectId
                                                           select new
                                                           {
                                                               user =  (from p in _context.Persons
                                                                       where p.Id == uq.PersonId
                                                                       select p)
                                                                   .Include(x => x.PrimaryPhoneNumber).FirstOrDefaultAsync(cancellationToken),
                                                               lovedones = (from profileGroup in _context.ProfileGroups where profileGroup.UserId == uq.Id
                                                                            join profileGroupDetail in _context.ProfileGroupDetails on profileGroup.Id equals profileGroupDetail.ProfileGroupId
                                                                            join person in _context.Persons on profileGroupDetail.PersonId equals person.Id
                                                                            select person).Include(u => u.PrimaryPhoneNumber)
                                                                   .ToListAsync(cancellationToken),
                                                           }).FirstOrDefaultAsync(cancellationToken);

and I am getting this error.

The LINQ expression 'DbSet<Person>()
    .Where(p => p.Id == u.PersonId)
    .Include(x => x.PrimaryPhoneNumber)
    .FirstOrDefaultAsync(__cancellationToken_1)' could no## Heading ##t 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'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information

Is there something I am doing wrong?

CodePudding user response:

Replace .ToListAsync(cancellationToken) with .ToList() and .FirstOrDefaultAsync(cancellationToken) with .FirstOrDefault(). It is wrong to use async methods in projection.

var userAndLovedOnesQuery = await (
    from uq in _context.Users
    where uq.SubjectId == request.SubjectId
    select new
    {
        user = (from p in _context.Persons
                where p.Id == uq.PersonId
                select p)
            .Include(x => x.PrimaryPhoneNumber)
            .FirstOrDefault(),
        lovedones = (from profileGroup in _context.ProfileGroups where profileGroup.UserId == uq.Id
                    join profileGroupDetail in _context.ProfileGroupDetails on profileGroup.Id equals profileGroupDetail.ProfileGroupId
                    join person in _context.Persons on profileGroupDetail.PersonId equals person.Id
                    select person).Include(u => u.PrimaryPhoneNumber)
            .ToList(),
    }).FirstOrDefaultAsync(cancellationToken);
  • Related