【问题标题】:EF Core join table with multiple condition with nullable property具有可为空属性的多个条件的 EF Core 联接表
【发布时间】:2020-04-13 00:08:48
【问题描述】:

我有以下表结构:

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

谢谢

【问题讨论】:

    标签: c# linq-to-sql entity-framework-core-2.1


    【解决方案1】:

    由于NotificationTypeIdDelegate 类型上可以为空,但在TrainingNotifcation 类型上不能为空,您可以将TrainingNotification.NotificationTypeId 强制转换为int?

    var delegates21 = await from tn in _context.TrainingNotification
                                join cd in _context.Delegate on
                                new { tn.DelegateId, (int?)tn.NotificationTypeId } equals
                                new { cd.DelegateId, cd.NotificationTypeId }
    

    应该没问题。

    【讨论】:

      【解决方案2】:

      这可能是因为您没有明确识别匿名类型的各个部分。试试:

       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 } 
      

      【讨论】:

      • 谢谢。无法加入 DelegateId 和 NotificationTypeId。这两列不同
      • 我没有看到在 TrainingNotification 上定义的 NotificationTypeId。它应该在那里吗?
      • 我已经更新了表格。抱歉错过了在 TrainingNotification 中添加 NotificationTypeId。
      • @MukilDeepthi 您是否尝试加入 2 个单独的 ID(delegateId 和 NotificationTypeId)?
      • 是的。我更新了我的查询,还更新了示例数据和预期结果。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-23
      • 2016-10-02
      • 2021-12-21
      • 2020-06-25
      • 2019-11-26
      • 2021-07-17
      相关资源
      最近更新 更多