Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将对象列表转换为每个数组索引有2个计数的对象列表数组?_C#_Asp.net_Arrays_List_Object - Fatal编程技术网

C# 如何将对象列表转换为每个数组索引有2个计数的对象列表数组?

C# 如何将对象列表转换为每个数组索引有2个计数的对象列表数组?,c#,asp.net,arrays,list,object,C#,Asp.net,Arrays,List,Object,我希望每个人在这场危机中都做得很好 好的,这是我的对象列表,对象包含名称和值: 我喜欢对这个对象列表做的是,我需要将它转换为 对象列表的数组,每个索引的数组计数为2。e、 g 当我们将列表的总计数除以2时,它就是8。 因此,我的数组将有4个索引,其中的列表包含对象。但在数组的每个索引上,每个索引的列表计数将为2 考虑第一个索引的上述示例: Array[1 first index (rest will be same)] => List[Count = 2 of object] =>

我希望每个人在这场危机中都做得很好

好的,这是我的对象列表,对象包含名称和值:

我喜欢对这个对象列表做的是,我需要将它转换为 对象列表的数组,每个索引的数组计数为2。e、 g

当我们将列表的总计数除以2时,它就是8。 因此,我的数组将有4个索引,其中的列表包含对象。但在数组的每个索引上,每个索引的列表计数将为2

考虑第一个索引的上述示例:

Array[1 first index (rest will be same)] => List[Count = 2 of object] => {object containg the name and value}

到目前为止,我所尝试的是通过分离键和值,并使用内部循环和其他一些方法循环计数2,但似乎无法使其满足我的需要。谢谢你的帮助

以下是一些示例代码,可以让您开始使用,但您需要验证初始列表是否有偶数:

// This is a representation your current list, the actual one is slightly different
var list = new List<AdditionalPropertyJsonModel>
{
    new AdditionalPropertyJsonModel {name = "one", value = "two"},
    new AdditionalPropertyJsonModel {name = "three", value = "four"},
    new AdditionalPropertyJsonModel {name = "five", value = "six"},
    new AdditionalPropertyJsonModel {name = "seven", value = "eight"},
    new AdditionalPropertyJsonModel {name = "nine", value = "ten"},
    new AdditionalPropertyJsonModel {name = "eleven", value = "twelve"},
};

// Create an array that's half the size of the list
var myArray = new List<AdditionalPropertyJsonModel>[list.Count / 2];

// Populate the array so that each item is a list of two items from the original list
for (var i = 0; i < myArray.Length; i ++)
{
    myArray[i] = new List<AdditionalPropertyJsonModel>
    {
        // Since each index of the array represents two items from the list, we
        // multiply the array index by 2 on each iteration to get the list indexes
        // of the values we want to add to this two-item list for the array index
        list[i * 2],
        list[i * 2 + 1]
    };
}
//这是您当前列表的表示形式,实际列表略有不同
变量列表=新列表
{
新的附加属性jsonmodel{name=“one”,value=“two”},
新的附加属性JsonModel{name=“three”,value=“four”},
新的附加属性jsonmodel{name=“five”,value=“six”},
新的附加属性jsonmodel{name=“seven”,value=“seek”},
新的附加属性jsonmodel{name=“nine”,value=“ten”},
新的附加属性jsonmodel{name=“十一”,value=“十二”},
};
//创建一个只有列表大小一半的数组
var myArray=新列表[List.Count/2];
//填充数组,使每个项目都是原始列表中两个项目的列表
for(var i=0;i
以下是一些示例代码,可以帮助您入门,但您需要验证初始列表是否有偶数:

// This is a representation your current list, the actual one is slightly different
var list = new List<AdditionalPropertyJsonModel>
{
    new AdditionalPropertyJsonModel {name = "one", value = "two"},
    new AdditionalPropertyJsonModel {name = "three", value = "four"},
    new AdditionalPropertyJsonModel {name = "five", value = "six"},
    new AdditionalPropertyJsonModel {name = "seven", value = "eight"},
    new AdditionalPropertyJsonModel {name = "nine", value = "ten"},
    new AdditionalPropertyJsonModel {name = "eleven", value = "twelve"},
};

// Create an array that's half the size of the list
var myArray = new List<AdditionalPropertyJsonModel>[list.Count / 2];

// Populate the array so that each item is a list of two items from the original list
for (var i = 0; i < myArray.Length; i ++)
{
    myArray[i] = new List<AdditionalPropertyJsonModel>
    {
        // Since each index of the array represents two items from the list, we
        // multiply the array index by 2 on each iteration to get the list indexes
        // of the values we want to add to this two-item list for the array index
        list[i * 2],
        list[i * 2 + 1]
    };
}
//这是您当前列表的表示形式,实际列表略有不同
变量列表=新列表
{
新的附加属性jsonmodel{name=“one”,value=“two”},
新的附加属性JsonModel{name=“three”,value=“four”},
新的附加属性jsonmodel{name=“five”,value=“six”},
新的附加属性jsonmodel{name=“seven”,value=“seek”},
新的附加属性jsonmodel{name=“nine”,value=“ten”},
新的附加属性jsonmodel{name=“十一”,value=“十二”},
};
//创建一个只有列表大小一半的数组
var myArray=新列表[List.Count/2];
//填充数组,使每个项目都是原始列表中两个项目的列表
for(var i=0;i
var myArray=新列表[List.Count/2]
Hi@RufusL,所以我尝试了你提到的上述代码。它将列表划分为我想要的数组,但数组中的列表为空。是的,您需要从列表填充它哦,对。谢谢你的帮助。我会尽力让你知道的。
var myArray=newlist[List.Count/2]
Hi@RufusL,所以我尝试了你提到的上述代码。它将列表划分为我想要的数组,但数组中的列表为空。是的,您需要从列表填充它哦,对。谢谢你的帮助。我会尽量让你知道的。太好了。它按我希望的那样工作。不知道为什么我没有想到这一点:完美。它按我希望的那样工作。不知道为什么我没有想到这个:P。