【发布时间】:2016-02-12 23:59:00
【问题描述】:
我有 2 个列表。第一个是包含一堆对象的主列表,每个对象都有时间戳(Datetime)、类型(int)、值(double)和标志(int)。第二个列表有类型(int)和描述(字符串)。
我的问题是我需要第一个列表在每个时间戳都有所有类型。在没有这样的对象匹配(时间戳,类型)的情况下,我需要插入一个带有(时间戳,类型,空值和空标志)的新对象。
我如何使用 Linq 做到这一点?
示例对象:
var date = DateTime.UtcNow;
var points = new List<Point> {
new Point { ts_utc = date , type = 300, value = 25 , flags = 0 },
new Point { ts_utc = date , type = 400, value = 250 , flags = 0 },
new Point { ts_utc = date , type = 500, value = 2500 , flags = 0 },
new Point { ts_utc = date.AddDays(1) , type = 300, value = 26 , flags = 0 },
//new Point { ts_utc = date.AddDays(1) , type = 400, value = 260, flags = 0 },
new Point { ts_utc = date.AddDays(1) , type = 500, value = 2600 , flags = 0 },
new Point { ts_utc = date.AddDays(2) , type = 300, value = 27 , flags = 0 },
new Point { ts_utc = date.AddDays(2) , type = 400, value = 270 , flags = 0 },
new Point { ts_utc = date.AddDays(2) , type = 500, value = 2700 , flags = 0 }
};
var desc = new List<Description> {
new Description{ type = 300, description = "300" },
new Description{ type = 400, description = "400" },
new Description{ type = 500, description = "500" },
};
var q = from p in points
join d in desc on p.type equals d.type into joined
from subd in joined.DefaultIfEmpty(...)
...
查询不完整。在上面的列表中,我将一个点作为该(时间戳、类型)组合缺失值的示例进行了注释。在那个地方,我想插入一个默认对象,其中包含现有类型的时间戳,但插入了缺少的类型。值和标志应该为空。我想 group by 可能会派上用场?
【问题讨论】:
标签: c# linq missing-data