foreach循环数据索引作为关键

时间:2016-01-09 08:51:19

标签: c# dictionary

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

我做了两个这样的课程

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

之后我尝试制作像这样的datacontainer列表

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

enter image description here

2 个答案:

答案 0 :(得分:1)

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

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

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

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

你有一个列表('harakatSininilMabsutoh'),其中每个元素都是一个DataContainer。列表中的每个DataContainer都有两个属性:索引和'DataValues'列表(NB在帖子中从'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。

答案 1 :(得分:1)

最简单的方法是使用LINQ FirstOrDefault这样的

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<List<int>>

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<int>(展平):

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

如果找不到FirstOrDefaultnull可能会返回userinput,请注意,如果您不使用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