【问题标题】:How to convert list item to multidimensional array in c#?如何在c#中将列表项转换为多维数组?
【发布时间】:2019-09-18 22:42:09
【问题描述】:

我有一个单一的数据列表,例如

List 0 index Contains : BlockId = 438001,DistrictId = 438,TownId = 0,UserId = 1077
UserTypeId = 4,VillageId = 238047,ZoneId = 1018
List 1 index contains : BlockId = 438001,DistrictId = 438,TownId = 0,UserId = 1077,UserTypeId = 4,VillageId = 238048,ZoneId = 1018
List 2 index contains : BlockId = 438002,DistrictId = 438,TownId = 0,UserId = 1077,UserTypeId = 4,VillageId = 238138,ZoneId = 1018

现在我想根据这个创建一个多维数组 public List<int>[][] arrayList{get;set;}

我需要将列表数据转换为多维数组列表,如

[0]
[0] -> Count 1 -> 438001  --> Showing blockId
[1]-> Count 3 -> 238047,238048 --> showing villageId


[1]
[0]->count 1 -> 438002
[1]->count 2 -> 238138
[2]->count 3-> 0 --> showing townId



var data = user.getUserRolebyUserId(userId);
var blkList = data.GroupBy(x => x.BlockId);
List<int>[][] lst;
foreach (var item in data.GroupBy(x => x.BlockId))
{
    List<int>[][] array = [item.Key,item.Select(x => x.VillageId).ToList(), item.Select(q => q.TownId)];
}

我正在尝试应用它,但它向我显示了无效表达式的错误。 那么如何将列表转换为多维数组呢?

【问题讨论】:

  • List&lt;int&gt;[][]不是 多维数组,它是List&lt;int&gt;锯齿状数组
  • 除了 TheGeneral 所说的,... = [something, something, something]; 甚至不是有效的 C# 语法。我想这可能表明需要花更多时间学习 C# 教程,涵盖如何创建、初始化和使用数组和集合的基础知识......
  • 我正在尝试List&lt;int&gt;[][] array = new int[][] {item.Key, item1.VillageId, item1.TownId };,但它也没有转换它。
  • 您声明了一个List&lt;int&gt;[][] 类型的变量,然后尝试创建并分配一个int[][] 实例给它。 (int[][] 甚至与List&lt;int&gt;[][] 完全不同,也不能转换为List&lt;int&gt;[][])什么?这没有一点意义......
  • @elgonzo,如何将简单的 int[][] 添加到 List[][] ?

标签: c# list arraylist multidimensional-array


【解决方案1】:

使用字典:

   class Program
    {
        static void Main(string[] args)
        {
            List<Location> locations = new List<Location>() {
                new Location() { BlockId = 438001,DistrictId = 438,TownId = 0,UserId = 1077, UserTypeId = 4,VillageId = 238047,ZoneId = 1018 },
                new Location() { BlockId = 438001,DistrictId = 438,TownId = 0,UserId = 1077,UserTypeId = 4,VillageId = 238048,ZoneId = 1018},
                new Location() { BlockId = 438002,DistrictId = 438,TownId = 0,UserId = 1077,UserTypeId = 4,VillageId = 238138,ZoneId = 1018 }
            };

            Dictionary<int, List<int>> dict = locations
                .GroupBy(x => x.BlockId, y => y.Items)
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());

        }
    }
    public class Location
    {
        public int BlockId { get; set; }
        public int DistrictId { get; set; }
        public int TownId { get; set; }
        public int UserId { get; set; }
        public int UserTypeId { get; set; }
        public int VillageId { get; set; }
        public int ZoneId { get; set; }

        public List<int> Items {
            get { return new List<int> { BlockId, DistrictId, TownId, UserId, UserTypeId, VillageId, ZoneId }; }
            set {;}
        }
    }

【讨论】:

  • 字典对我来说是新的。我可以将此字典转换为 List[][] 吗?
  • 您确实有三个级别的数组(不是两个),因为您使用的是 GroupBy。 GroupBy 创建一个二维数组 [key][List]。在您的情况下,列表是一组属性(BlockId、DistrictId、TownId,...)。那么,当您说 List[][] 时,您真的要删除“BlockId”之类的属性名称吗?
  • 是的,实际上我需要 Int 列表,其中包含两个数组,其中第一个包含村 ID,第二个包含城镇 ID。我正在搜索将字典转换为 List[][] 但几乎只使用 int[][].
猜你喜欢
  • 2013-05-17
  • 2019-06-25
  • 2012-04-04
  • 2016-09-24
  • 1970-01-01
  • 1970-01-01
  • 2021-04-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多