【问题标题】:foreach loop data index as the keyforeach 循环数据索引作为键
【发布时间】:2016-01-09 08:51:19
【问题描述】:

我一直在尝试以索引为键为每个循环 这种情况下,如果输入用户与索引匹配,我想制定一个逻辑,我将显示所有以索引为键的数据

我这样做了两堂课

class DataContainer
    {

        public DataContainer()
        {

        }


        public int index { get; set; }
        public List<DataValue> DataValue { get; set; }
    }
    class DataValue
    {
        public DataValue()
        {

            IntegerValues = new List<int>();
        }
        public string name { get; set; }
        public List<int> IntegerValues { get; set; }
    }

之后我尝试制作这样的数据容器列表

List<DataContainer> harakatSininilMabsutoh = new List<DataContainer>(){
        new DataContainer{index = 2015 , DataValue = new List<DataValue>()
         {
             new DataValue{name = "first",IntegerValues = {9,55,18,11}},
             new DataValue{name = "second"   ,IntegerValues = {5,54,18,11}},
             new DataValue{name = "third" ,IntegerValues = {40,26,14,11}},
             new DataValue{name = "four"    ,IntegerValues = {22,0,5,10}},
             new DataValue{name = "fifth"    ,IntegerValues = {46,44,17,0}},

         }
         },
          new DataContainer{index = 2013 , DataValue = new List<DataValue>()
         {
             new DataValue{name = "first",IntegerValues = {26,49,8,11}},
             new DataValue{name = "second"   ,IntegerValues = {19,42,8,11}},
             new DataValue{name = "third" ,IntegerValues = {55,3,12,11}},
             new DataValue{name = "fourth"    ,IntegerValues = {27,4,23,8}},
             new DataValue{name = "fifth"    ,IntegerValues = {43,22,7,1}},

         }
         },
          new DataContainer{index = 2001, DataValue = new List<DataValue>()
         {
             new DataValue{name = "first",IntegerValues = {35,44,27,10}},
             new DataValue{name = "second"   ,IntegerValues = {24,41,27,10}},
             new DataValue{name = "third" ,IntegerValues = {36,30,26,10}},
             new DataValue{name = "fourth"    ,IntegerValues = {59,24,8,6}},
             new DataValue{name = "fifth"    ,IntegerValues = {29,27,26,1}},

         }
          }
   };

然后我做了一个这样的逻辑

    int years = (this is user input);
    if(years == 2015)
{
    ///How to for each this which has index 2015
}
else if (years = 2013)
{
         //how to foreach this which has index 2013
}
else if (years = 2001)
{
         //how to foreach this which has index 2001

【问题讨论】:

  • 您面临的问题是什么?
  • 那么问题是什么?
  • 我想循环以index为key的数据

标签: c# dictionary


【解决方案1】:

最简单的就是像这样使用LINQFirstOrDefault

int userinput = 2015;
DataContainer requested = harakatSininilMabsutoh.FirstOrDefault(x => x.index == userinput);
if (requested == null) //FirstOrDefault of a class will return null if not found
    return;
foreach (DataValue val in requested.DataValue) 
    Console.WriteLine(val.name + ": " + string.Join(", ", val.IntegerValues));          

编辑 2:

如果您只需要所有整数,没有名称,没有其他任何东西,那么您可以这样做以获得List&lt;List&lt;int&gt;&gt;

int userinput = 2015;
List<List<int>> intValuesOnly = harakatSininilMabsutoh
    .FirstOrDefault(x => x.index == userinput)
    .DataValue
    .Select(y => y.IntegerValues)
    .ToList();
//Do whatever you want with intValuesOnly. This is everything that you have in a list of lists

或者这样做得到List&lt;int&gt;(扁平化):

int userinput = 2015;
List<int> intValuesOnly = harakatSininilMabsutoh
    .FirstOrDefault(x => x.index == userinput)
    .DataValue
    .SelectMany(y => y.IntegerValues)
    .ToList();
//Do whatever you want with intValuesOnly. This is everything that you have in single list

如果userinput 未找到,FirstOrDefault 可能会返回 null,请注意,如果您不使用 C#6,则可能需要考虑两步 LINQ:

int userinput = 2015;
DataContainer requested = harakatSininilMabsutoh.FirstOrDefault(x => x.index == userinput);
if (requested == null) //FirstOrDefault of a class will return null if not found
    return;
List<List<int>> intValuesOnly = requested
    .Select(y => y.IntegerValues)
    .ToList();
//Do whatever you want with intValuesOnly. This is everything that you have in a list of lists

【讨论】:

  • 我不想得到名字,我只想得到整数值,如果你能的话,我会让你接受答案
  • "我只想获取整数值" 你的意思是在请求中?还是在控制台的写作中?还是两者兼而有之?
  • 是的,我只想显示获取整数值,没有字符串连接
  • 我明白了,您不想打印出来是吗?只是一切的整数值。你说的是这个吗?
  • @ridwan 我不确定答案是否是您真正想要的,但您可以看看是否是。我已经编辑过了。
【解决方案2】:

首先,请注意,在这一行中,您尝试使用类型名称作为属性名称:

public List<DataValue> DataValue { get; set; }

我已将此属性重命名为“DataValues”,如下所示:

public List<DataValue> DataValues { get; set; }

您有一个列表 ('harakatSininilMabsutoh'),其中的每个元素都是一个 DataContainer。列表中的每个 DataContainer 都有两个属性:一个索引和一个“DataValues”列表(注意从您帖子中的“DataValue”重命名)。

因此,您想要的循环逻辑将是这样的:

var matchingYears = harakatSininilMabsutoh.Where(h => h.index == years);

foreach (var dataContainer in matchingYears)
{
    foreach (var item in dataContainer.DataValues)
    {
        // Action required on each DataValue:
        Debug.Print(item.name + " " + string.Join(",", item.IntegerValues));
    }
}

您需要在您的类中添加以下“using”语句,因为“Where”是一种 LINQ 扩展方法:

using System.Linq;

如果您知道恰好有一个匹配年份,您可以添加First() 并删除外部foreach 循环。如果您知道最多会有一个匹配年份(但可能为零),您仍然可以删除外部 foreach 循环,但您应该改用 FirstOrDefault() 并测试 null。

【讨论】:

  • 我不想得到名字,我只想得到整数值,如果你能我会让你接受答案
  • 正如@Ian 所说,听起来你根本不需要循环......他编辑的答案几乎涵盖了它。
猜你喜欢
  • 2016-07-14
  • 1970-01-01
  • 2011-06-02
  • 2017-10-10
  • 2018-04-14
  • 1970-01-01
  • 2021-02-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多