有一种情况,我将从oracle数据库中获取数据。 行的格式-ID,名称,金额,数据1,数据2,日期以及其他列。
表中填充了外部数据,并且为每个条目创建了行,因此相同的名称可以有多个条目
对于给定的名称,仅在金额更改时才需要选择行。
因此数据可能是:
var data = new []
{
new { Id = 1, Name = "John", Amount = 1000, Data_1 = 123, Data_2 = 234, Date = new DateTime(2018, 1, 1) },
new { Id = 2, Name = "John", Amount = 1000, Data_1 = 123, Data_2 = 234, Date = new DateTime(2018, 1, 2) },
new { Id = 3, Name = "John", Amount = 2000, Data_1 = 123, Data_2 = 234, Date = new DateTime(2018, 1, 3) },
new { Id = 4, Name = "John", Amount = 2000, Data_1 = 112, Data_2 = 113, Date = new DateTime(2018, 1, 4) },
};
我需要选择ID-1和3。 由于金额列的值已从前一列id-2更改,因此id-1是第一行,而id-3是。
如果我没有正确解释问题,请告诉我。
感谢您的帮助。
答案 0 :(得分:2)
您可以这样操作:
var data = new []
{
new { Id = 1, Name = "John", Amount = 1000, Data_1 = 123, Data_2 = 234, Date = new DateTime(2018, 1, 1) },
new { Id = 2, Name = "John", Amount = 1000, Data_1 = 123, Data_2 = 234, Date = new DateTime(2018, 1, 2) },
new { Id = 3, Name = "John", Amount = 2000, Data_1 = 123, Data_2 = 234, Date = new DateTime(2018, 1, 3) },
new { Id = 4, Name = "John", Amount = 2000, Data_1 = 112, Data_2 = 113, Date = new DateTime(2018, 1, 4) },
};
var result = data.Aggregate(data.Take(0).ToList(), (a, x) =>
{
if (!a.Any() || a.Last().Amount != x.Amount)
{
a.Add(x);
}
return a;
});
产生:
答案 1 :(得分:-1)
按要避免重复选择的列进行分组,然后选择按ID排序的分组中的第一项。在您的情况下,我认为是Name,Amount