Wie kann ich eine Summierung mithilfe von LINQ mit IQueryable in Entity Framework Core ausführen?
Posted: 18 Jan 2025, 19:17
Ich arbeite an einem Projekt mit Entity Framework Core 3.1 und muss die Gesamterfahrung eines Kandidaten in Jahren zusammenfassen. In meinem Code habe ich eine CvExperiences-Tabelle mit den Feldern „StartDate“ und „EndDate“ und möchte die Gesamterfahrung für jeden Kandidaten in Jahren berechnen.
Hier ist der Teil davon Code, in dem ich versuche, die Erfahrung zu berechnen:
Dieser Ansatz funktioniert nicht, wenn ich Experience-Ergebnisse haben möchte, aber ohne Experience hat es gut funktioniert. Der Fehler zeigt:
Hier heißt es, dass LINQ nicht direkt in SQL übersetzt werden kann. Wie mache ich das?
Hier ist der Teil davon Code, in dem ich versuche, die Erfahrung zu berechnen:
Code: Select all
public class GetCandidateCVBankListQuery : DataFetchModel, IRequest
{
public class Handler : IRequestHandler
{
private readonly IHrmService hrmService;
public Handler(IHrmService hrmService) => this.hrmService = hrmService;
public async Task Handle(GetCandidateCVBankListQuery request, CancellationToken cancellationToken)
{
var query = hrmService.Context.CvPersonalInformations
.Include(x => x.CvCertifications)
.Include(x => x.CvEducations)
.Include(x => x.CvEmergencyContacts)
.Include(x => x.CvExperiences)
.Include(x => x.CvSkills)
.Include(x => x.CvReferenceDetails)
.Include(x => x.CvProfessionalQualifications)
.OrderByDescending(x => x.CvEducations.Any(e => e.IsHighestEducation.HasValue))
.Select(x => new
{
CandidateName = x.FullName,
HigherstEducation = x.CvEducations.Select(e => e.EducationLevel).FirstOrDefault(),
CGPA = x.CvEducations.Select(e => e.Result).FirstOrDefault(),
Institute = x.CvEducations.Select(e => e.InstitutionName).FirstOrDefault(),
Age = x.DateOfBirth.HasValue ? (DateTime.Now.Year - x.DateOfBirth.Value.Year -
(DateTime.Now.DayOfYear < x.DateOfBirth.Value.DayOfYear ? 1 : 0)) : (int?)null,
Experience = x.CvExperiences
.Where(e => e.StartDate.HasValue && e.EndDate.HasValue)
.Sum(e => (e.EndDate.Value - e.StartDate.Value).TotalDays / 365.25),
HomeDistrict = x.HomeDistrict
})
.AsQueryable();
return await this.hrmService.GetListViewDataAsync(query, request, cancellationToken);
}
}
}
Code: Select all
The LINQ expression '(EntityShaperExpression:
EntityType: CvExperience
ValueBufferExpression:
ProjectionBindingExpression: EmptyProjectionMember
IsNullable: False
.EndDate.Value - EntityShaperExpression:
EntityType: CvExperience
ValueBufferExpression:
ProjectionBindingExpression: EmptyProjectionMember
IsNullable: False
.StartDate.Value).TotalDays / 365.25' 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 https://go.microsoft.com/fwlink/?linkid=2101038 for more information.The LINQ expression '(EntityShaperExpression:
EntityType: CvExperience
ValueBufferExpression:
ProjectionBindingExpression: EmptyProjectionMember
IsNullable: False
.EndDate.Value - EntityShaperExpression:
EntityType: CvExperience
ValueBufferExpression:
ProjectionBindingExpression: EmptyProjectionMember
IsNullable: False
.StartDate.Value).TotalDays / 365.25' 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 https://go.microsoft.com/fwlink/?linkid=2101038 for more information.