我正在尝试将一系列对象列表添加到新列表中,但收到以下消息:
Cannot implicitly convert type "void" to "System.Collections.Generic.List"
这是我正在尝试的:
Detalhes = new List<DetalhesExcel>() {
new DetalhesExcel()
{
NumColunaInicial = 23,
Coluna1 = "Representante",
Coluna2 = "ValorRepasse",
Coluna3 = "KmRepasse"
}
}.AddRange(ocorrencias.Where(x => x.Conclusao != "PRÓPRIO")
.GroupBy(x => x.Representante)
.Select(x => new DetalhesExcel
{
NumColunaInicial = 23,
Coluna1 = x.First().Representante,
Coluna2 = x.Sum(y => y.ValorRepasse).ToString(),
Coluna3 = x.Sum(y => y.KmRepasse).ToString()
}).ToList())
我需要做什么?
详细Excel类:
public class DetalhesExcel
{
public int NumColunaInicial { get; set; }
public string Coluna1 { get; set; }
public string Coluna2 { get; set; }
public string Coluna3 { get; set; }
public string Coluna4 { get; set; }
public string Coluna5 { get; set; }
}
最佳答案
我想你想要的是这样的
class Program
{
static void Main(string[] args)
{
List<Ocorrencia> ocorrencias = new List<Ocorrencia>() {
new Ocorrencia() {
Conclusao = "teste",
KmRepasse = 1,
Representante = "um",
ValorRepasse = 2
}
};
var Detalhes = new List<DetalhesExcel>() {
new DetalhesExcel()
{
NumColunaInicial = 23,
Coluna1 = "Representante",
Coluna2 = "ValorRepasse",
Coluna3 = "KmRepasse"
}
}.Concat(ocorrencias.Where(x => x.Conclusao != "PRÓPRIO")
.GroupBy(x => x.Representante)
.Select(x => new DetalhesExcel
{
NumColunaInicial = 23,
Coluna1 = x.First().Representante,
Coluna2 = x.Sum(y => y.ValorRepasse).ToString(),
Coluna3 = x.Sum(y => y.KmRepasse).ToString()
}).ToList());
}
}
public class Ocorrencia
{
public string Conclusao { get; set; }
public string Representante { get; set; }
public int KmRepasse { get; set; }
public int ValorRepasse { get; set; }
}
public class DetalhesExcel
{
public int NumColunaInicial { get; set; }
public string Coluna1 { get; set; }
public string Coluna2 { get; set; }
public string Coluna3 { get; set; }
public string Coluna4 { get; set; }
public string Coluna5 { get; set; }
}
关于c# - 将范围添加到同一命令行/行中的新对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54812605/