【问题标题】:Reading a list inside an object with lambda使用 lambda 读取对象内的列表
【发布时间】:2017-10-14 17:25:49
【问题描述】:

我有这些物品:

public class HistoricoDTO
{
    public long EquipoId { get; set; }
    public double Valor { get; set; }
    public string Fecha { get; set; }
    public string Dia { get;set;}
    public string Mes { get; set; }
    public string Ano { get; set; }

    public List<HistoricoVariableDTO> ValorVariable { get; set; }
}


public class HistoricoVariableDTO
{
    public string Variable { get; set; }
    public double Valor { get; set; }
}

我需要在属性ValorVariable 中获取一些值,我需要使用 Lambda 获取 Valor,但它不起作用,我无法做到。

我正在尝试做这样的事情:

foreach (var item in HistoricoData.Where(x => x.EquipoId.Equals(EquipoIdSelected) && x.Ano.Equals(AnoSelected) && x.ValorVariable.Any(y => y.Variable.Contains(VariableSlected))))

我需要访问列表,我不知道使用这个是否正确:

x.ValorVariable.Any(y => y.Variable.Contains(VariableSlected))

谢谢!

【问题讨论】:

  • 问题不清楚。你没办法做什么?你有错误吗?你有什么问题?
  • 告诉我们你的目标是什么,什么是行不通的

标签: c# list lambda


【解决方案1】:

您需要先将结果存储在一个变量中,然后使用 AsEnumerable() 循环通过该输出变量,例如:

var output = HistoricoData.Where(x => x.EquipoId.Equals(EquipoIdSelected) && x.Ano.Equals(AnoSelected) && x.ValorVariable.Any(y => y.Variable.Contains(VariableSlected)))

foreach(var item in output.AsEnumerable())//loop through your list here
{....}

【讨论】:

  • 不,你不需要。
猜你喜欢
  • 2020-01-21
  • 2020-11-29
  • 2021-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-31
  • 2019-07-10
相关资源
最近更新 更多