【发布时间】:2019-12-12 13:52:00
【问题描述】:
我的查询 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();
在我的模型中,我使用虚拟道具来填写外键属性 DeliveryType。 它看起来像这样(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
}
]
但是,我有多个相同 NotificationGroupId 的记录,我希望列出如下:
[
{
"notificationGroupId": 4,
"name": "Trial",
"defaultDeliveryType": {
"deliveryTypeId": 1,
"name": "SMS"
},
"defaultDeliveryType": {
"deliveryTypeId": 2,
"name": "Email"
},
"hasChoosen": true
}
]
请注意“notificationGroupId”的区别:4,它需要有嵌套的传递类型。
更新 #1 我设法实现了一些目标,但我仍然需要使用 Select 投影仪语句映射到我的模型。
这是一个例子:
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 });
更新 #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; }
}
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; }
}
}
这是我通过 EF Core 映射的模型描述:
【问题讨论】:
-
你不能只使用
notificationGroupId的分组吗?此外,defaultDeliveryType必须更改为列表。因为如果您分组,每个 notificationGroup 可以有多个。 -
@jpgrassi 是的!有用。但我没有映射到我的模型我只有匿名属性。请看我的更新#1
-
在
.GroupBy之后,您可以执行.Select并投影到您需要的任何内容。创建反映所需 JSON 的新模型并相应地填充它们。 -
是的,但我还需要一个名称才能从 GroupBy 中提取,我该怎么做?
-
@Stefan89BEG 你的名字在这里
DeliveryTypes = g如果你只想要名字,它会是DeliveryTypes = g.Select(x => x.name)但他不是你想要的输出。
标签: c# asp.net-mvc linq asp.net-core