我想有一个LINQ查询,它应该返回所有VitalSigns成员,其中生命体征中的事件等于手术。
我的Member.cs
课程:
public class Member
{
public int Id { get; set; }
public string FullName { get; set; }
public ICollection<VitalSign> VitalSigns { get; set; }
public Member()
{
VitalSigns = new Collection<VitalSign>();
}
}
我的VitalSign.cs
课程是:
public class VitalSign
{
public int Id { get; set; }
public string Event { get; set; }
// relationships
public Member Member { get; set; }
public int MemberId { get; set; }
}
我写的LINQ查询是:
return await context. Members.Include(c => c.VitalSigns.Where(t => t.Event == "post surgery")).ToListAsync();
这将返回一个自引用循环。因为VitalSigns
中有一些数据,其中事件不等于“手术后”。我写的查询错了吗?
答案 0 :(得分:4)
查询应为:
context.Members.Where(t => t.VitalSigns.Any(u => u.Event == "post surgery"))
.Include(c => c.VitalSigns)
.ToListAsync()
Include()
只是在执行查询时应该加载哪些表的提示。
查询类似于:
all the members WHERE there is ANY (at least) one VitalSign with Event == post surgery
together with the Members you'll get, please INCLUDE the VitalSigns (the alternative is that they'll be lazily loaded when you try to access them)
return a List<> (ToListAsync) of the elements in an asynchronous way