【问题标题】:Add range to a new list of object within the same command / line将范围添加到同一命令/行中的新对象列表
【发布时间】:2019-02-22 01:43:24
【问题描述】:

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

无法将类型“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; }
}

【问题讨论】:

  • 请把你的 DetalhesExcel 类放在代码上
  • @JoãoPauloAmorim DetalhesExcel 类结构与它无关。
  • @Sumitraj 我只是想在这里重新创建它,抱歉
  • @joão-paulo-amorim 好的,我已经在课堂上更新了它。
  • .AddRange 需要单独调用,因为它返回void。改用Concat

标签: c# list linq


【解决方案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; }
}

【讨论】:

  • @Taian 很好,很高兴为您提供帮助
【解决方案2】:

AddRange 返回无效。从而将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())

【讨论】:

  • 没有办法“合并”它们吗? “Detalhes”是另一个对象的属性,我不想把它分开。
【解决方案3】:

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

【讨论】:

    【解决方案4】:

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

    【讨论】:

      【解决方案5】:

      根据您对 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"
          }
          };
      

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

      【讨论】:

        猜你喜欢
        • 2021-08-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-24
        • 1970-01-01
        • 2015-07-22
        • 2014-03-12
        相关资源
        最近更新 更多