C# 使用LINQ数据透视
我从数据库中以这种形式获取数据:C# 使用LINQ数据透视,c#,linq,C#,Linq,我从数据库中以这种形式获取数据: Item Collection_Period Value ==== ================= ===== Item3 201307 27.2 Item4 201308 19 Item3 201209 2.1 Item2 201307 345 Item1 201309 13.11 Ite
Item Collection_Period Value
==== ================= =====
Item3 201307 27.2
Item4 201308 19
Item3 201209 2.1
Item2 201307 345
Item1 201309 13.11
Item2 201308 34
Item3 200609 85
Item4 201308 58.2
Item3 201209 2.4
Item2 201309 12.1
Item1 201209 12.3
我需要以这种方式操作数据:
Item CurrMon-3 CurrMon-2 CurrMon-1
===== ========= ========= =========
Item1 13.11
Item2 345 34 12.1
Item3 27.2
Item4 19 58.2
(只需显示最近三个月的数据)。我正在尝试:
public List<PivotedMean> AssociateResultsWithCollectionDate(List<MeanData> meanData)
{
var pivoted = new List<PivotedMean>();
var currentMonth = DateTime.Now.Month;
var results = meanData
.GroupBy(i => i.Description)
.Select(g => new
{
Description = g.Key,
month3 = g.Where(c => c.CollPeriod.Month == currentMonth - 3),
month2 = g.Where(c => c.CollPeriod.Month == currentMonth - 2),
month1 = g.Where(c => c.CollPeriod.Month == currentMonth - 1)
});
return pivoted;
}
虽然
PivotedMean
类似乎与Linq查询输出一致,但当我将var results=
替换为pivoted=时,我得到了一个错误,这是因为PivotedMean
是一个PivotedMean列表,Linq查询(在您的例子中)返回一个匿名类的IEnumerable
- 您可以在LINQ查询的末尾添加
.ToList()
,以便将其计算到列表中
- 您可以将其映射到
PivotedMean
实例,而不是匿名对象
例如:
public List<PivotedMean> AssociateResultsWithCollectionDate(List<MeanData> meanData)
{
var currentMonth = DateTime.Now.Month;
return meanData
.GroupBy(i => i.Description)
.Select(g => new PivotedMean // notice the class name here
{ // this is now a PivotedMean initializer
Description = g.Key, // You were also missing some caps here
Month3 = g.Where(c => c.CollPeriod.Month == currentMonth - 3),
Month2 = g.Where(c => c.CollPeriod.Month == currentMonth - 2),
Month1 = g.Where(c => c.CollPeriod.Month == currentMonth - 1)
}).ToList(); // notice the ToList
}
public List AssociateResultsWithCollectionDate(列表指数据)
{
var currentmount=DateTime.Now.Month;
返回均值数据
.GroupBy(i=>i.Description)
.Select(g=>newpivotedmean//注意这里的类名
{//这现在是一个PivotedMean初始值设定项
Description=g.Key,//您还缺少一些大写字母
Month3=g.Where(c=>c.CollPeriod.Month==currentMonth-3),
Month2=g.Where(c=>c.CollPeriod.Month==currentMonth-2),
Month1=g.Where(c=>c.CollPeriod.Month==currentMonth-1)
}).ToList();//注意ToList
}
或者,尽可能使用IEnumerable
s而不是List
s。它们提供了对“经历事物”的抽象
编码时,根据接口而不是实现进行编码被认为是最佳做法,因为您只关心并依赖于元素的行为,而不是它们的实际类型。这是因为pivoted
是一个PivotedMean列表和一个LINQ查询(在您的例子中)返回匿名类的IEnumerable
- 您可以在LINQ查询的末尾添加
.ToList()
,以便将其计算到列表中
- 您可以将其映射到
PivotedMean
实例,而不是匿名对象
例如:
public List<PivotedMean> AssociateResultsWithCollectionDate(List<MeanData> meanData)
{
var currentMonth = DateTime.Now.Month;
return meanData
.GroupBy(i => i.Description)
.Select(g => new PivotedMean // notice the class name here
{ // this is now a PivotedMean initializer
Description = g.Key, // You were also missing some caps here
Month3 = g.Where(c => c.CollPeriod.Month == currentMonth - 3),
Month2 = g.Where(c => c.CollPeriod.Month == currentMonth - 2),
Month1 = g.Where(c => c.CollPeriod.Month == currentMonth - 1)
}).ToList(); // notice the ToList
}
public List AssociateResultsWithCollectionDate(列表指数据)
{
var currentmount=DateTime.Now.Month;
返回均值数据
.GroupBy(i=>i.Description)
.Select(g=>newpivotedmean//注意这里的类名
{//这现在是一个PivotedMean初始值设定项
Description=g.Key,//您还缺少一些大写字母
Month3=g.Where(c=>c.CollPeriod.Month==currentMonth-3),
Month2=g.Where(c=>c.CollPeriod.Month==currentMonth-2),
Month1=g.Where(c=>c.CollPeriod.Month==currentMonth-1)
}).ToList();//注意ToList
}
或者,尽可能使用IEnumerable
s而不是List
s。它们提供了对“经历事物”的抽象
当您编码时,根据接口而不是实现编码被认为是最佳实践,因为您只关心并依赖于元素的行为,而不是它们的实际类型