Taian
Taian

Reputation: 319

Add range to a new list of object within the same command / line

I'm trying to add a range of a list of objects to a new list, but I'm getting the following message:

Cannot implicitly convert type "void" to "System.Collections.Generic.List"

Here is what I'm trying:

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

What do I need to do?


DetalhesExcel Class:

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

Upvotes: 2

Views: 3623

Answers (5)

Rufus L
Rufus L

Reputation: 37020

Since the AddRange modifies the existing sequence and returns void, it cannot be used in a method chain as part of an assignment. However, you can use the Concat method, which creates a new sequence by concatenating the first with the second, and then returns that new sequence:

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

Upvotes: 1

Gibbon
Gibbon

Reputation: 2773

Based on the comment on Simonare's answer that you made, have you tried doing

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

essentially using the selected list in the list based constructor

Upvotes: 0

Jo&#227;o Paulo Amorim
Jo&#227;o Paulo Amorim

Reputation: 436

I think what you want is something like this

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

Upvotes: 2

Sumit raj
Sumit raj

Reputation: 831

AddRange() returns void and that' the final method you are applying on your list. You cannot assign that to a 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())

Upvotes: 0

Derviş Kayımbaşıoğlu
Derviş Kayımbaşıoğlu

Reputation: 30545

AddRange returns void. Thus seperate List initialization and AddRange into two.

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

Upvotes: 1

Related Questions