简体   繁体   English

返工 linq 查询以返回嵌套对象

[英]rework linq query to returns nested objects

Im having a db table like this我有一个这样的数据库表数据库图像

And my query linq looks like我的查询 linq 看起来像

   var selectedNotifications = _dbContext.UserNotificationTypeDeliveryChoice
    .Include(m => m.NotificationGroup)
    .Include(m => m.DeliveryType)
    .Where(m => m.UserDefId == userDefId && m.UserTypeId == (int)userType)
    .Select(m => new NotificationGroup()
    {
      NotificationGroupId = m.NotificationGroup.NotificationGroupId,
      Name = m.NotificationGroup.Name,
      DefaultDeliveryType = m.DeliveryType,
      HasChoosen = true
    }).ToList();

In my model I used virtual prop to fill in foreign key property DeliveryType.在我的模型中,我使用虚拟道具来填充外键属性 DeliveryType。 It looks like this (JSON):它看起来像这样(JSON):

[
  {
    "notificationGroupId": 1,
    "name": "Comments",
    "defaultDeliveryType": {
      "deliveryTypeId": 2,
      "name": "Email"
    },
    "hasChoosen": true
  },
  {
    "notificationGroupId": 2,
    "name": "Q&A",
    "defaultDeliveryType": {
      "deliveryTypeId": 2,
      "name": "Email"
    },
    "hasChoosen": true
  },
  {
    "notificationGroupId": 3,
    "name": "Services",
    "defaultDeliveryType": {
      "deliveryTypeId": 2,
      "name": "Email"
    },
    "hasChoosen": true
  },
  {
    "notificationGroupId": 4,
    "name": "Trial",
    "defaultDeliveryType": {
      "deliveryTypeId": 2,
      "name": "Email"
    },
    "hasChoosen": true
  },
  {
    "notificationGroupId": 4,
    "name": "Trial",
    "defaultDeliveryType": {
      "deliveryTypeId": 1,
      "name": "SMS"
    },
    "hasChoosen": true
  }
]

However, I have multiple records for same NotificationGroupId, and I would like to have list as follows:但是,对于同一个 NotificationGroupId,我有多个记录,我想列出如下列表:

[
  {
    "notificationGroupId": 4,
    "name": "Trial",
    "defaultDeliveryType": {
      "deliveryTypeId": 1,
      "name": "SMS"
    },
    "defaultDeliveryType": {
        "deliveryTypeId": 2,
        "name": "Email"
      },
    "hasChoosen": true
  }
]

Please notice the difference for "notificationGroupId": 4, it need to has nested delivery types.请注意“notificationGroupId”的区别:4,它需要有嵌套的传递类型。

Update #1 I managed to achieve something, but still I need to map to my model with Select projector statement.更新 #1我设法实现了一些目标,但我仍然需要使用 Select 投影仪语句映射到我的模型。

Here is an example:下面是一个例子:

var selectedNotifications = _dbContext.UserNotificationTypeDeliveryChoice
                            .Include(m => m.NotificationGroup)
                            .Include(m => m.DeliveryType)
                            .Where(m => m.UserDefId == userDefId && m.UserTypeId == (int)userType)
                            .GroupBy(p => p.NotificationGroupId,
                                     p => p.DeliveryType,
                                     (key, g) => new { NotificationGroupId = key, DeliveryTypes = g });

Update #2 strong text更新 #2强文本

    public class UserNotificationTypeDeliveryChoice
    {
        public List<NotificationGroup> NotificationGroups { get; set; }
        //public List<DeliveryType> DeliveryTypes { get; set; }
        public long UserNotificationTypeDeliveryChoiceId { get; set; }
        public int? UserDefId { get; set; }
        public int? UserCompanyOrInstitutionId { get; set; }
        public byte NotificationGroupId { get; set; }
        public byte DeliveryTypeId { get; set; }
        public int UserTypeId { get; set; }
        public virtual DeliveryType DeliveryType { get; set; }
        public virtual NotificationGroup NotificationGroup { get; set; }
        public virtual UserDef UserDef { get; set; }
    }

and Model for NotificationGroup and DeliveryType:和 NotificationGroup 和 DeliveryType 的模型:

    public class NotificationGroup
    {
        public NotificationGroup()
        {
            UserNotificationTypeDeliveryChoice = new HashSet<UserNotificationTypeDeliveryChoice>();
            NotificationGroupUserType = new HashSet<NotificationGroupUserType>();
        }
        //public List<DeliveryType> DeliveryTypes { get; set; }
        public byte NotificationGroupId { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        //public bool HasChoosen { get; set; }
        public virtual DeliveryType DefaultDeliveryType { get; set; }
        public byte DefaultDeliveryTypeId { get; set; }
        public virtual ICollection<UserNotificationTypeDeliveryChoice> UserNotificationTypeDeliveryChoice { get; set; }
        public virtual ICollection<NotificationGroupUserType> NotificationGroupUserType { get; set; }
    }

    public class DeliveryType
    {
        public DeliveryType()
        {
            NotificationGroup = new HashSet<NotificationGroup>();
            UserNotificationTypeDeliveryChoice = new HashSet<UserNotificationTypeDeliveryChoice>();
            NotificationGroupUserType = new HashSet<NotificationGroupUserType>();
        }

        public byte DeliveryTypeId { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }

        public virtual ICollection<NotificationGroup> NotificationGroup { get; set; }
        public virtual ICollection<UserNotificationTypeDeliveryChoice> UserNotificationTypeDeliveryChoice { get; set; }
        public virtual ICollection<NotificationGroupUserType> NotificationGroupUserType { get; set; }
    }
}

Here is my model description which is mapped through EF Core:这是我通过 EF Core 映射的模型描述:

If you have a non-unique ID, you cannot group by NotificationGroup, you have to stick with ID.如果您有一个非唯一 ID,则不能按 NotificationGroup 分组,您必须坚持使用 ID。

To keep the Name of NotifigrationGroup, you have to keep the NotificationGroup as the TElement of the GroupBy.要保留 NotifigrationGroup 的名称,您必须保留 NotificationGroup 作为 GroupBy 的 TElement。 (That's why p=>p.) (这就是为什么 p=>p。)

Later in the Result selector, you can pick a name from one of the elements, just pick the name of the first NotificationGroup.稍后在 Result 选择器中,您可以从其中一个元素中选取一个名称,只需选取第一个 NotificationGroup 的名称即可。 And here you can Select the DeliveryTypes, if you like.如果您愿意,您可以在此处选择 DeliveryTypes。

var selectedNotifications = _dbContext.UserNotificationTypeDeliveryChoice
                            .Include(m => m.NotificationGroup)
                            .Include(m => m.DeliveryType)
                            .Where(m => m.UserDefId == userDefId && m.UserTypeId == (int)userType)
                            .GroupBy( p => p.NotificationGroupId, p => p, 
                    (key,g) => new { 
                         NotificationId = key, 
                         Name = g.First().Name, 
                         DeliveryTypes = g.Select(x => x.DeliveryTypes)
                               }););

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM