我在一个对象中有三个列表,想使用 LINQ 按操作顺序执行
包含列表的对象
public class ApplicationCommunications
{
public ApplicationCommunications()
{
listNotification = new List<ApplicationNotifications>();
listEmail = new List<ApplicationEmail>();
listSMS = new List<ApplicationSMS>();
}
public List<ApplicationNotifications> listNotification { get; set; }
public List<ApplicationEmail> listEmail { get; set; }
public List<ApplicationSMS> listSMS { get; set; }
}
从数据库中获取数据
ApplicationCommunications applicationCommunications = new ApplicationCommunications();
applicationCommunications.listNotification = GetApplicationNotification(applicationId).Select(c => new ApplicationNotifications
{
NotificationId = c.NotificationId,
Message = c.Message,
SendDate = c.SendDate.Value
}).ToList();
applicationCommunications.listEmail = GetApplicationEmails(applicationId).Select(t => new ApplicationEmail
{
EmailContent = t.Body,
EmailAddress = t.Email,
SendDate = t.SendDate.Value,
}).ToList();
applicationCommunications.listSMS = GetApplicationMessage(applicationId).Select(t => new ApplicationSMS
{
SMSContent = t.Body,
PhoneNumber = t.Phone,
SendDate = t.SendDate.Value,
}).ToList();
我们有三个列表,每个对象列表都有“senddate”属性,现在我想从这三个列表中创建一个新列表,我们将按顺序排列数据。这可能吗?
我们如何通过发送日期执行订单?只是我想按顺序显示数据。
答案 0 :(得分:3)
4096m
方法为您提供 Select
类型的列表。 Enumerable
可以由 Enumerable
订购,所以只需这样做
OrderBy
编辑: 您可以在此处阅读更多信息https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.orderby?view=net-5.0
答案 1 :(得分:1)
请原谅不完整的问题。我们有三个列表,每个列表 对象现在具有“senddate”属性,我想从中创建一个新列表 这三个列表我们将按顺序列出数据。这可能吗?
如另一个答案所示,您需要 OrderBy
:
List<DateTime> orderedSendDates = applicationCommunications.listNotification
.Select(x => x.NotificationSendDate)
.Concat(applicationCommunications.listEmail.Select(x => x.EmailSendDate))
.Concat(applicationCommunications.listSMS.Select(x => x.SMSSendDate))
.OrderBy(dt => dt)
.ToList();
如果您想要唯一的 DateTime
,请在 Distinct
之前使用 OrderBy
。
如果您在需要列表时没有初始化这些属性,您可以这样做:
List<DateTime> orderedSendDates =
GetApplicationNotification(applicationId).Select(x => x.SendDate)
.Concat(GetApplicationEmails(applicationId).Select(x => x.SendDate))
.Concat(GetApplicationMessage(applicationId).Select(x => x.SendDate))
.Where(sendDateOrNull => sendDateOrNull.HasValue)
.Select(sendDateOrNull => sendDateOrNull.Value)
.OrderBy(dt => dt)
.ToList();
答案 2 :(得分:1)
如果您想要一个包含不同类型元素的大列表,按 sendDate 排序(但不仅仅是 dateTime 列表),您可以首先为此创建一个通用类型:
public class SentElement {
public string ElementDescription {get ; set;}
public DateTime SendDate { get; set;}
}
然后使用 Select
将您的不同类型映射到通用类型,以您想要的方式为每种类型的元素填充描述:
var listNotification = GetApplicationNotification(applicationId).Select(c => new SentElement
{
ElementDescription = c.NotificationId + c.Message,
SendDate= c.SendDate.Value
}).ToList();
var listEmail = GetApplicationEmails(applicationId).Select(t => new SentElement
{
ElementDescription = t.EmailContent + t.EmailAddress,
SendDate = t.SendDate.Value,
}).ToList();
var listSMS = GetApplicationMessage(applicationId).Select(t => new SentElement
{
ElementDescription = t.Body + t.Phone,
SendDate = t.SendDate.Value,
}).ToList();
最后对结果进行合并和排序:
var mergedList = listNotification.Concat(listEmail).Concat(listSMS).OrderByDescending(t=> t.SendDate);