【发布时间】:2019-07-15 20:41:52
【问题描述】:
如何用重复值更新元素值?
假设我的列表中有 4 个元素,如您所见,有 Dog1 和 Dog2 元素:
名称:Dog1 价格:NULL 年龄:14 ImportDate = 14.07.2019 15:00
名称:Dog1 价格:NULL 年龄:17 ImportDate = 14.07.2019 14:00
名称:Dog1 价格:14.00 年龄 = 13 ImportDate = 14.07.2019 13:00
名称:Dog2 价格:NULL 年龄:14 ImportDate = 14.07.2019 16:00
名称:Dog2 价格:NULL 年龄:17 ImportDate = 14.07.2019 10:00
名称:Dog2 价格:22.00 年龄 = 13 ImportDate = 14.07.2019 09:00
在列表中的这些元素中,我只想保留最新 ImportDate 的狗,同时我想保留列表中的这两个元素:
名称:Dog1 价格:NULL 年龄:14 ImportDate = 14.07.2019 15:00
名称:Dog2 价格:NULL 年龄:14 ImportDate = 14.07.2019 16:00
在下面,我提供了一条代码线,将这两条狗保留在列表中,并删除其余的。
dogList
.GroupBy(x => new {
x.Name,
x.ImportDate.Date
})
.Select(g => g
.OrderByDescending(x => x.ImportDate)
.First())
.ToList();
但是我保留在列表中的两条狗没有任何价格。 我想做的是为这些价值为 NULL 的狗设置价格,以便在特定日期对现有狗有价格,在这种情况下,最新的 dog1 应该有 14:00 的价格,而 dog2 应该有价格 22.
我怎样才能做到这一点? 我想我需要从列表中找到元素,然后找到狗的名字和 importdate 的价格。然后用结算更新列表中的元素
更新列表中的值后,结果应如下所示:
名称:Dog1 价格:14.00 年龄:14 ImportDate = 14.07.2019 15:00
名称:Dog1 价格:14.00 年龄:17 ImportDate = 14.07.2019 14:00
名称:Dog1 价格:14.00 年龄 = 13 ImportDate = 14.07.2019 13:00
名称:Dog2 价格:22.00 年龄:14 ImportDate = 14.07.2019 16:00
名称:Dog2 价格:22.00 年龄:17 ImportDate = 14.07.2019 10:00
名称:Dog2 价格:22.00 年龄 = 13 ImportDate = 14.07.2019 09:00
最终结果应该是包含这些元素的列表:
名称:Dog1 价格:14.00 年龄:14 ImportDate = 14.07.2019 15:00
名称:Dog2 价格:22.00 年龄:14 ImportDate = 14.07.2019 16:00
【问题讨论】: