EFCore:根据相关实体中某字段的最大值查询相关实体
EFCore: Query a related entity based on the maximum value in a field in the related entity
我需要根据相关实体中字段中的最大值查询相关实体,然后显示该项目的结果。
例如模型定义为:
public class Student
{
public int StudentID {get; set;}
public string Name {get; set;}
public ICollection<ReportCard> ReportCards {get; set;}
}
public class ReportCard
{
public int ReportCardID {get; set;}
public int ProjectID { get; set; }
public Project Project { get; set; }
public int Result {get; set;}
public int Comment {get; set;}
public DateTime PublishDate {get; set;}
}
在剃须刀控制器中:
public class LatestResultsModel : PageModel
{
...
public IList<Student> Students {get; set;}
public async Task<IActionResult> OnGetAsync()
{
Students = await _context.Student
.Include(student => student.ReportCard)
.ToListAsync();
}
}
在剃刀视图中:
@foreach (Student student in Model.Students)
{
<p>@student.Name</p>
<p>@student.ReportCard.Max(i => i.PublishDate).Result.ToString()</p>
}
Max语句后无法查询其他字段。
我已经尝试了一些方法来实现过滤相关数据的结果。
Filtered Includes are not supported.
是否有某种类型的联接可以实现此结果?
它也不处理学生没有 ReportCard 的情况。 InvalidOperationException:可为空的对象必须有一个值。
After the Max statement I cannot query the other fields. I've tried a few things to achieve this outcome of filtering the related data.
是的!你不能!因为 Max
语句 select 只有你在 Max
.
中提到的字段
It also doesn't handle the situation when a Student has no ReportCard. InvalidOperationException: Nullable object must have a value.
按照以下步骤克服这两个问题:
@foreach (Student student in Model.Students)
{
<p>@student.Name</p>
if(student.ReportCards.Count > 0)
{
<p>@student.ReportCards.OrderByDescending(rc => rc.PublishDate).FirstOrDefault().Result.ToString()</p>
<p>@student.ReportCards.OrderByDescending(rc => rc.PublishDate).FirstOrDefault().PublishDate.ToString()</p>
}
else
{
<p>Student has no report card!</p>
}
}
我需要根据相关实体中字段中的最大值查询相关实体,然后显示该项目的结果。
例如模型定义为:
public class Student
{
public int StudentID {get; set;}
public string Name {get; set;}
public ICollection<ReportCard> ReportCards {get; set;}
}
public class ReportCard
{
public int ReportCardID {get; set;}
public int ProjectID { get; set; }
public Project Project { get; set; }
public int Result {get; set;}
public int Comment {get; set;}
public DateTime PublishDate {get; set;}
}
在剃须刀控制器中:
public class LatestResultsModel : PageModel
{
...
public IList<Student> Students {get; set;}
public async Task<IActionResult> OnGetAsync()
{
Students = await _context.Student
.Include(student => student.ReportCard)
.ToListAsync();
}
}
在剃刀视图中:
@foreach (Student student in Model.Students)
{
<p>@student.Name</p>
<p>@student.ReportCard.Max(i => i.PublishDate).Result.ToString()</p>
}
Max语句后无法查询其他字段。 我已经尝试了一些方法来实现过滤相关数据的结果。
Filtered Includes are not supported.
是否有某种类型的联接可以实现此结果?
它也不处理学生没有 ReportCard 的情况。 InvalidOperationException:可为空的对象必须有一个值。
After the Max statement I cannot query the other fields. I've tried a few things to achieve this outcome of filtering the related data.
是的!你不能!因为 Max
语句 select 只有你在 Max
.
It also doesn't handle the situation when a Student has no ReportCard. InvalidOperationException: Nullable object must have a value.
按照以下步骤克服这两个问题:
@foreach (Student student in Model.Students)
{
<p>@student.Name</p>
if(student.ReportCards.Count > 0)
{
<p>@student.ReportCards.OrderByDescending(rc => rc.PublishDate).FirstOrDefault().Result.ToString()</p>
<p>@student.ReportCards.OrderByDescending(rc => rc.PublishDate).FirstOrDefault().PublishDate.ToString()</p>
}
else
{
<p>Student has no report card!</p>
}
}