【问题标题】:return a list-object from a dictionary [closed]从字典中返回一个列表对象[关闭]
【发布时间】:2014-03-18 07:02:58
【问题描述】:

我如何返回字典中的所有列表条目

public class Meter
{
    public string MeterID { get; set; }
    public List<Data> data { get; set; }
}

public class Data
{
    public string Time { get; set; }
    public int Signal { get; set; }
}

Meter 是一个字典字符串,Meter 这个我试过了

private static Dictionary<string, Meter> meters = new Dictionary<string, Meter>();
public static List<Meter> GetDataList()
{
    return meters.Where(g => g.Key.Equals(Data)).Select(g => g.Value).ToList();
}

【问题讨论】:

  • 什么是米?仪表类的集合?
  • 字典是Dictionary&lt;string, Meter&gt; 还是Dictionary&lt;Data, Meter&gt;?不清楚。
  • 您究竟需要从仪表中选择什么?所有数据的组合列表(作为单个List&lt;Data&gt;)?
  • meters 是 Meter 和 Dictionary 的集合,最好使用单一列表

标签: c# linq dictionary collections


【解决方案1】:

为所有仪表选择单个数据列表:

public static List<Data> GetDataList()
{
    return meters.Values.SelectMany(m => m.data).ToList();
}

【讨论】:

  • 谢谢 :) 就像一个魅力
猜你喜欢
  • 1970-01-01
  • 2016-07-25
  • 2014-10-15
  • 2021-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-22
  • 1970-01-01
相关资源
最近更新 更多