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

时间:2019-02-21 17:08:46

标签: c# list linq

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

  

无法将类型“ void”隐式转换为“ 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())

我需要做什么?


DetalhesExcel类别:

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; }
}

5 个答案:

答案 0 :(得分:1)

AddRange返回void。因此,将List初始化和AddRange分为两个。

Detalhes = new List<DetalhesExcel>() {
    new DetalhesExcel()
    {
        NumColunaInicial = 23,
        Coluna1 = "Representante",
        Coluna2 = "ValorRepasse",
        Coluna3 = "KmRepasse"
    }
};

Detalhes.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())

答案 1 :(得分:1)

我认为您想要的就是这样

    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; }
}

答案 2 :(得分:1)

由于AddRange修改了现有序列并返回了void,因此它不能在方法链中用作赋值的一部分。但是,您可以使用Concat方法,该方法通过将第一个与第二个连接起来来创建一个新序列,然后返回该新序列:

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());

答案 3 :(得分:0)

AddRange()返回void,这是您在list上应用的最终方法。 您不能将其分配给list

Detalhes = new List<DetalhesExcel>() {
    new DetalhesExcel()
    {
        NumColunaInicial = 23,
        Coluna1 = "Representante",
        Coluna2 = "ValorRepasse",
        Coluna3 = "KmRepasse"
    }
};
Detalhes.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())

答案 4 :(得分:0)

根据您对Simonare的回答所作的评论,您是否尝试过

Detalhes = new List<DetalhesExcel>(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())
    {
    new DetalhesExcel()
    {
        NumColunaInicial = 23,
        Coluna1 = "Representante",
        Coluna2 = "ValorRepasse",
        Coluna3 = "KmRepasse"
    }
    };

本质上使用基于列表的构造函数中的选定列表