C# 基于集合中现有对象的属性,使用LINQ将对象插入到集合中
我有一个对象集合,其中包含如下数据: FromTime Duration 2010-12-28 24.0000 2010-12-29 24.0000 2010-12-30 24.0000 2010-12-31 22.0000 2011-01-02 1.9167 2011-01-03 24.0000 2011-01-04 24.0000 2011-01-05 24.0000 2011-01-06 24.0000 2011-01-07 22.0000 2011-01-09 1.9167 2011-01-10 24.0000 定时持续时间 2010-12-28 24.0000 2010-12-29 24.0000 2010-12-30 24.0000 2010-12-31 22.0000 2011-01-02 1.9167 2011-01-03 24.0000 2011-01-04 24.0000 2011-01-05 24.0000 2011-01-06 24.0000 2011-01-07 22.0000 2011-01-09 1.9167 2011-01-10 24.0000 在“FromTime”列中,存在数据“缺口”,即2011-01-01和2011-01-08“缺失”。因此,我想做的是循环一系列日期(在本例中为2010-12-28到2011-01-10),并“填充”持续时间为0的“缺失”数据 因为我刚刚开始使用LINQ,我觉得它应该“相当”简单,但我不能完全正确地使用它。我正在读《LINQ在行动》一书,但觉得在解决这个问题之前我还有很长的路要走。因此,任何帮助都将不胜感激C# 基于集合中现有对象的属性,使用LINQ将对象插入到集合中,c#,.net,linq,C#,.net,Linq,我有一个对象集合,其中包含如下数据: FromTime Duration 2010-12-28 24.0000 2010-12-29 24.0000 2010-12-30 24.0000 2010-12-31 22.0000 2011-01-02 1.9167 2011-01-0
大卫是那种人。我没有测试它,但我相信,你会得到这个想法:
var data = new[]
{
new { Date = DateTime.Now.AddDays(-5), Duration = 3.56 },
new { Date = DateTime.Now.AddDays(-3), Duration = 3.436 },
new { Date = DateTime.Now.AddDays(-1), Duration = 1.56 },
};
Func<DateTime, DateTime, IEnumerable<DateTime>> range = (DateTime from, DateTime to) =>
{
List<DateTime> dates = new List<DateTime>();
from = from.Date;
to = to.Date;
while (from <= to)
{
dates.Add(from);
from = from.AddDays(1);
}
return dates;
};
var result = range(data.Min(e => e.Date.Date), data.Max(e => e.Date.Date))
.Join(data, e => e.Date.Date, e => e.Date, (d, x) => new {
Date = d,
Duration = x == null
? 0.0
: x.Duration
});
var数据=新[]
{
新{Date=DateTime.Now.AddDays(-5),Duration=3.56},
新{Date=DateTime.Now.AddDays(-3),Duration=3.436},
new{Date=DateTime.Now.AddDays(-1),Duration=1.56},
};
Func范围=(日期时间从,日期时间到)=>
{
列表日期=新列表();
from=from.日期;
to=截止日期;
while(从e.Date.Date开始),data.Max(e=>e.Date.Date))
.Join(数据,e=>e.Date.Date,e=>e.Date,(d,x)=>new{
日期=d,
持续时间=x==null
? 0.0
:x.持续时间
});
另外,最好用一些静态方法替换这个范围的lambda。我将像下面这样定义类:
public class DurDate
{
public DateTime date = DateTime.ToDay;
public decimal dure = 0;
}
private IEnumerable<DurDate> GetAllDates(IEnumerable<DurDate> lstDur)
{
var min = lstDur.Min(x => x.date).Date;
var max = lstDur.Max(x => x.date).Date;
var nonexistenceDates = Enumerable.Range(0, (int) max.Subtract(min).TotalDays)
.Where(x =>!lstDur.Any(p => p.date.Date == min.Date.AddDays(x)))
.Select(p => new DurDate {date = min.Date.AddDays(p), dure = 0});
return lstDur.Concat(nonexistenceDates).OrderBy(x=>x.date);
}
并将编写如下函数:
public class DurDate
{
public DateTime date = DateTime.ToDay;
public decimal dure = 0;
}
private IEnumerable<DurDate> GetAllDates(IEnumerable<DurDate> lstDur)
{
var min = lstDur.Min(x => x.date).Date;
var max = lstDur.Max(x => x.date).Date;
var nonexistenceDates = Enumerable.Range(0, (int) max.Subtract(min).TotalDays)
.Where(x =>!lstDur.Any(p => p.date.Date == min.Date.AddDays(x)))
.Select(p => new DurDate {date = min.Date.AddDays(p), dure = 0});
return lstDur.Concat(nonexistenceDates).OrderBy(x=>x.date);
}
哪些天数不在给定范围内:
Where(x =>!lstDur.Any(p => p.date.Date == min.Date.AddDays(x)))
找到这些天之后,我将选择它们:
Select(p => new DurDate {date = min.Date.AddDays(p), dure = 0})
最后,将初始值连接到此列表(并对其进行排序):
为什么要在数据集中乱丢空条目?很不幸,我必须这样做,因为我要将此数据绑定到第三方Silverlight组件,这要求我这样做,以便生成的图形看起来像样。是否应该考虑时间组件?例如,您已经在集合中有
2011-01-02 22:00:00.000
。2011-01-02 00:00:00.000
是否应被视为缺失?不,时间部分并不重要。我会清理OP以简化问题。@DavidS:说得好,但请确保您可以在数据绑定之前插入空条目。我在数据库中看不到它们的任何位置。谢谢Saeed。您的代码工作正常,您对这些内容的解释也不重要他编写的代码非常明确,特别是针对像我这样的LINQ初学者。很抱歉没有升级,但我现在已经这么做了。:。顺便问一下,除了现有的内容之外,还有什么建议吗?你认为学习LINQ的最佳资源是什么?@DavidS,对创业很有好处,之后如果你想提高技能,你应该读一些书,比如感谢您花时间阅读问题并提供答案。代码有效,但Saeed的答案对我来说更容易理解,这就是为什么我选择他的答案优先于您的答案。
lstDur.Concat(nonexistenceDates).OrderBy(x=>x.date);