代碼拋出此例外 - 我該如何解決?
System.InvalidOperationException: LINQ 運算式 'GroupByShaperExpression: KeySelector: (Nullable)d.DiseasesCategoryId, ElementSelector:EntityShaperExpression: EntityType: Diagnosis ValueBufferExpression: ProjectionBindingExpression: EmptyProjectionMember IsNullable: False .GroupBy(p.翻譯。以可翻譯的形式重寫查詢,或通過插入對“AsEnumerable”、“AsAsyncEnumerable”、“ToList”或“ToListAsync”的呼叫,顯式切換到客戶端評估。有關詳細資訊,請參閱https://go.microsoft.com/fwlink/?linkid=2101038。
代碼:
IQueryable<Diagnosis> diagnoses = _context.Diagnosis
.Include(d => d.Patient)
.Include(d => d.Prescription);
IQueryable<Diagnosis> diagnosesWithSpecification = diagnoses
.Where(d => d.Prescription.UpdatedOn.Year == year &&
d.Prescription.UpdatedOn.Month == month &&
d.Prescription.HospitalId == hospitalId);
int currentYear = DateTime.Now.Date.Year;
var groupByDiseases = await diagnosesWithSpecification
.GroupBy(dCat => dCat.DiseasesCategoryId)
.Select(x => new
{
DiseasesCategoryId = x.Key,
Gender = x.GroupBy(p => p.Patient.Gender).Select(y => new
{
Gender = y.Key,
ZeroToFive = y.Count(p => (currentYear - p.Patient.DoB.Value.Date.Year) >= 0 && (currentYear - p.Patient.DoB.Value.Date.Year) <= 5),
SixToFifteen = y.Count(p => (currentYear - p.Patient.DoB.Value.Date.Year) >= 6 && (currentYear - p.Patient.DoB.Value.Date.Year) <= 15),
SixteenToThirty = y.Count(p => (currentYear - p.Patient.DoB.Value.Date.Year) >= 16 && (currentYear - p.Patient.DoB.Value.Date.Year) <= 30),
ThirtyOneToFourtyFive = y.Count(p => (currentYear - p.Patient.DoB.Value.Date.Year) >= 31 && (currentYear - p.Patient.DoB.Value.Date.Year) <= 45),
FourtySixToSixty = y.Count(p => (currentYear - p.Patient.DoB.Value.Date.Year) >= 46 && (currentYear - p.Patient.DoB.Value.Date.Year) <= 60),
SixtyOnePlus = y.Count(p => (currentYear - p.Patient.DoB.Value.Date.Year) > 60),
Total = y.Count()
}).ToList(),
TotalPatient = x.Count()
}).ToListAsync();
uj5u.com熱心網友回復:
.ToList()在呼叫 none 資料庫函式之前使用。
diagnosesWithSpecification.ToList().GroupBy(dCat => dCat.DiseasesCategoryId)...
uj5u.com熱心網友回復:
您需要在客戶端分組。在此之前需要對回傳的結果集進行簡化。
var data = await _context.Diagnosis
.Where(d => d.Prescription.UpdatedOn.Year == year &&
d.Prescription.UpdatedOn.Month == month &&
d.Prescription.HospitalId == hospitalId)
.Select(d = new
{
d.DiseasesCategoryId,
d.Patient.Gender,
p.Patient.DoB
})
.ToListAsync();
int currentYear = DateTime.Now.Date.Year;
var groupByDiseases = data
.GroupBy(dCat => dCat.DiseasesCategoryId)
.Select(x => new
{
DiseasesCategoryId = x.Key,
Gender = x.GroupBy(p => p.Gender).Select(y => new
{
Gender = y.Key,
ZeroToFive = y.Count(p => (currentYear - p.DoB.Value.Date.Year) >= 0 && (currentYear - p.DoB.Value.Date.Year) <= 5),
SixToFifteen = y.Count(p => (currentYear - p.DoB.Value.Date.Year) >= 6 && (currentYear - p.DoB.Value.Date.Year) <= 15),
SixteenToThirty = y.Count(p => (currentYear - p.DoB.Value.Date.Year) >= 16 && (currentYear - p.DoB.Value.Date.Year) <= 30),
ThirtyOneToFourtyFive = y.Count(p => (currentYear - p.DoB.Value.Date.Year) >= 31 && (currentYear - p.DoB.Value.Date.Year) <= 45),
FourtySixToSixty = y.Count(p => (currentYear - p.DoB.Value.Date.Year) >= 46 && (currentYear - p.DoB.Value.Date.Year) <= 60),
SixtyOnePlus = y.Count(p => (currentYear - p.DoB.Value.Date.Year) > 60),
Total = y.Count()
}).ToList(),
TotalPatient = x.Count()
}).ToList();
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/402048.html
