c# - 具有可为空属性的多个条件的 EF Core 联接表
问题描述
我有以下表结构:
public class Delegate {
public int DelegateId {get;set;}
public int? NotificationTypeId { get; set; }
public NotificationType NotificationType { get; set; }
}
public class TrainingNotification {
public int TrainingNotificationId {get;set;}
public int NotificationTypeId {get;set;}
public int DelegateId {get;set;}
public virtual Delegate Delegate {get;set;}
}
Delegate 和 TrainingNotification 之间的一对多
public class NotificationType {
public int NotificationTypeId {get;set;}
public virtual ICollection<Delegate> Delegates {get;set;}
}
想要在 Delegate 中检索 NotificationTypeId 的 TrainingNotification。
var delegates21 = await from tn in _context.TrainingNotification
join cd in _context.Delegate on
new { tn.DelegateId, tn.NotificationTypeId } equals
new { cd.DelegateId, cd.NotificationTypeId }
但是在不正确的连接子句中获取表达式之一的错误类型
谁能帮助解决这个问题?
这是测试数据和预期结果:
Delegate:
DelegateId NotificationTypeId
100 1
8201 2
101 null
TrainginNotification:
TrainignNotificationId DelegateId NotificationTypeId
1 8201 1
2 8201 2
3 100 1
NotificationType:
NotificationTypeId Name
1 InviteEmail
2 ReminderEmail
Retrieve users who hasnt got reminder emails:
Result:
DelegateId
100
谢谢
解决方案
这可能是因为您没有明确识别匿名类型的各个部分。尝试:
var delegates21 = await from tn in _context.TrainingNotification
join cd in _context.Delegate on
new { did = tn.DelegateId, nid = tn.NotificationTypeId } equals
new { did = cd.DelegateId, nid = cd.NotificationTypeId }
推荐阅读
- html - bootstrap navbar-toggler 按钮单击将列表项颜色和大小更改为默认值一秒钟?
- python - 我怎样才能把这个 Python 代码变成一个函数?
- python - tkinter 文件目录保存输出
- xaml - 在图像文件名中使用限定符在 UWP 运行时不起作用
- wordpress - 无法为我的 Wordpress 网站更新 siteurl 和 home
- python - Python迭代器在数据框或带有股票数据的列表上
- sql-server - 使用 AVG 和 MAX 更新 SQL Server
- python - 从列中删除 NaN 值时会发生什么?
- python - 在 EC2 实例 (AWS) 上使用 Bash 脚本导入错误加载模块
- javascript - Json 到 Javascript 对象数组