sql - 在 LINQ 中包含带有 where 子句的对象
问题描述
我想要一个 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
的事件不等于“术后”。我写错了查询吗?
解决方案
查询应该是:
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
推荐阅读
- spring-boot - 无法访问 Openshift 上已部署的 Spring Boot 应用程序 (war) 的端点
- python - BS4 在 Python 中没有定位元素
- python - 如何从数据帧生成频率表?
- bash - 如何使用 bash 在 Windows 上的 VSC 中自动激活 conda env?
- html - 不同表格单元格中的两个选择选项不起作用
- cmake - CMake 设置变量
- python - Python 中的 JSON 转储在文件中写入换行符和回车符。
- c++ - 在构造 CString 时使用有符号或无符号字符?
- swift - Swift 变异函数作为第一类值
- python-3.x - 我们如何使用 jupyter notebook 在 python 3.7 下运行代码?