admin管理员组文章数量:1320661
I'm working on a project using Entity Framework Core 3.1, and I need to sum the total experience of a candidate in years. In my code, I have a CvExperiences table with StartDate and EndDate fields, and I want to calculate the total Experience
for each candidate in years.
Here is the part of the code where I try to calculate the Experience
:
public class GetCandidateCVBankListQuery : DataFetchModel, IRequest<ListViewModel>
{
public class Handler : IRequestHandler<GetCandidateCVBankListQuery, ListViewModel>
{
private readonly IHrmService hrmService;
public Handler(IHrmService hrmService) => this.hrmService = hrmService;
public async Task<ListViewModel> 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);
}
}
}
This approach does not work when I want Experience
results, but without Experience
, it worked fine. The error shows:
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 /?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 /?linkid=2101038 for more information.
Here it is said that LINQ cannot be directly translated into SQL. How do I do this?
I'm working on a project using Entity Framework Core 3.1, and I need to sum the total experience of a candidate in years. In my code, I have a CvExperiences table with StartDate and EndDate fields, and I want to calculate the total Experience
for each candidate in years.
Here is the part of the code where I try to calculate the Experience
:
public class GetCandidateCVBankListQuery : DataFetchModel, IRequest<ListViewModel>
{
public class Handler : IRequestHandler<GetCandidateCVBankListQuery, ListViewModel>
{
private readonly IHrmService hrmService;
public Handler(IHrmService hrmService) => this.hrmService = hrmService;
public async Task<ListViewModel> 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);
}
}
}
This approach does not work when I want Experience
results, but without Experience
, it worked fine. The error shows:
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/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/fwlink/?linkid=2101038 for more information.
Here it is said that LINQ cannot be directly translated into SQL. How do I do this?
Share Improve this question edited Jan 18 at 16:43 Showyeab Ahmed asked Jan 18 at 16:14 Showyeab AhmedShowyeab Ahmed 1611 silver badge10 bronze badges2 Answers
Reset to default 2You can translate the built-in function in SQL Server (DATEDIFF
):
Experience = x.CvExperiences
.Where(e => e.StartDate.HasValue && e.EndDate.HasValue)
.Sum(e =>
EF.Functions.DateDiffDay(e.StartDate.Value, e.EndDate.Value) / 365.0
)
EF Core translates this to DATEDIFF(day, StartDate, EndDate)
, then divides by 365.0 to get approximate years.
Because EF Core 3.1 can't translate your C# calculations of differences between days into SQL.
I think you can also use AsEnumerable but you have to know that this will make all the calculations in memory , so If you have a very huge data , that will make some latency in your query , Here it is the code
Experience = x.CvExperiences
.Where(e => e.StartDate.HasValue && e.EndDate.HasValue)
.AsEnumerable()
.Sum(e => (e.EndDate.Value - e.StartDate.Value).TotalDays / 365.25);
本文标签: cHow can I execute a summation utilizing LINQ with IQueryable in Entity Framework CoreStack Overflow
版权声明:本文标题:c# - How can I execute a summation utilizing LINQ with IQueryable in Entity Framework Core? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742065383a2418802.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论