c# - 将范围添加到同一命令行/行中的新对象列表

标签 c# list linq

我正在尝试将一系列对象列表添加到新列表中,但收到以下消息:

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/

相关文章:

c# - 调用方法时处理空对象

c# - 多参数对象类型

r - R中将长列表分成指定长度的短列表

c# - Entity Framework 5.0。我的查询有什么问题?

c# - Dynamic Linq - 连接具有一对多关系的表

c# - 从 LINQ 查询结果中获取最低年份

c# - 如何在 MVC 3 Controller 中获取动态创建的 Json 数据集?

c# - 如何对齐控件并保持其相对于相当大的窗体的大小?

python - 替换主词典中的用户输入列表元素?

python - 如何将多个 excel 行放入一个带有子列表的大列表中?