C# 将Linq查询求和为一行
我需要一种方法将多行linq查询组合成一行,并对所有合并行求和C# 将Linq查询求和为一行,c#,oracle,linq,C#,Oracle,Linq,我需要一种方法将多行linq查询组合成一行,并对所有合并行求和 where alldates.years <= Now() 需要作为 col1 col2 clo3 col4 col5 col6 1) 2015 6 2 2 2 10 我需要具体的分组方法。 提前感谢下面的代码应该会产生您正在寻找的输出。我在主方法中使用了测试数据 public class Result { public DateTime co
where alldates.years <= Now()
需要作为
col1 col2 clo3 col4 col5 col6
1) 2015 6 2 2 2 10
我需要具体的分组方法。
提前感谢下面的代码应该会产生您正在寻找的输出。我在主方法中使用了测试数据
public class Result
{
public DateTime col1 { get; set; }
public int col2 { get; set; }
public int col3 { get; set; }
public int col4 { get; set; }
public int col5 { get; set; }
}
public static void Main()
{
List<Result> listTest = new List<Result>();
listTest.Add(new Result() { col1 = DateTime.Now, col2 =1, col3 =3, col4 =5, col5=11});
listTest.Add(new Result() { col1 = DateTime.Now.AddYears(-1), col2 = 1, col3 = 3, col4 = 5, col5 = 11 });
listTest.Add(new Result() { col1 = DateTime.Now.AddYears(-1), col2 = 1, col3 = 3, col4 = 5, col5 = 11 });
var maxYearLessThanCurrentYear = listTest.Where(x => x.col1.Year < DateTime.Now.Year).Select(x => x.col1.Year).Max();
var output = listTest.Where(x => x.col1.Year < DateTime.Now.Year)
.Select( x => new { x.col2, x.col3, x.col4, x.col5, maxYearLessThanCurrentYear })
.GroupBy(x=>x.maxYearLessThanCurrentYear)
.Select(x => new Result
{
col1 = new DateTime(x.Key, 1, 1) ,
col2 = x.Sum(y => y.col2),
col3 = x.Sum(y => y.col3),
col4 = x.Sum(y => y.col4),
col5 = x.Sum(y => y.col5),
})
.FirstOrDefault();
}
公共类结果
{
公共日期时间col1{get;set;}
公共int col2{get;set;}
公共int col3{get;set;}
公共int col4{get;set;}
公共int col5{get;set;}
}
公共静态void Main()
{
List listTest=新列表();
Add(新结果(){col1=DateTime.Now,col2=1,col3=3,col4=5,col5=11});
Add(新结果(){col1=DateTime.Now.AddYears(-1),col2=1,col3=3,col4=5,col5=11});
Add(新结果(){col1=DateTime.Now.AddYears(-1),col2=1,col3=3,col4=5,col5=11});
var maxYearLessThanCurrentYear=listTest.Where(x=>x.col1.Yearx.col1.Year).Max();
var output=listTest.Where(x=>x.col1.Yearnew{x.col2,x.col3,x.col4,x.col5,maxYearLessThanCurrentYear})
.GroupBy(x=>x.maxYearLessThanCurrentYear)
.选择(x=>新结果
{
col1=新的日期时间(x.Key,1,1),
col2=x.Sum(y=>y.col2),
col3=x.Sum(y=>y.col3),
col4=x.Sum(y=>y.col4),
col5=x.Sum(y=>y.col5),
})
.FirstOrDefault();
}
您可能需要自己改进代码格式。还有可能充实您使用的代码?中的哪个列用于分组?抱歉,我应该声明分组是针对所有返回的行的,因此它是分组的总和,将返回所提供记录中的最高日期
public class Result
{
public DateTime col1 { get; set; }
public int col2 { get; set; }
public int col3 { get; set; }
public int col4 { get; set; }
public int col5 { get; set; }
}
public static void Main()
{
List<Result> listTest = new List<Result>();
listTest.Add(new Result() { col1 = DateTime.Now, col2 =1, col3 =3, col4 =5, col5=11});
listTest.Add(new Result() { col1 = DateTime.Now.AddYears(-1), col2 = 1, col3 = 3, col4 = 5, col5 = 11 });
listTest.Add(new Result() { col1 = DateTime.Now.AddYears(-1), col2 = 1, col3 = 3, col4 = 5, col5 = 11 });
var maxYearLessThanCurrentYear = listTest.Where(x => x.col1.Year < DateTime.Now.Year).Select(x => x.col1.Year).Max();
var output = listTest.Where(x => x.col1.Year < DateTime.Now.Year)
.Select( x => new { x.col2, x.col3, x.col4, x.col5, maxYearLessThanCurrentYear })
.GroupBy(x=>x.maxYearLessThanCurrentYear)
.Select(x => new Result
{
col1 = new DateTime(x.Key, 1, 1) ,
col2 = x.Sum(y => y.col2),
col3 = x.Sum(y => y.col3),
col4 = x.Sum(y => y.col4),
col5 = x.Sum(y => y.col5),
})
.FirstOrDefault();
}