Как преобразовать элемент списка в многомерный массив в C #? - PullRequest
0 голосов
/ 30 апреля 2019

У меня есть один список данных, например

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

Я пытаюсь применить это, но он показывает ошибку для неверного выражения.Итак, как мне преобразовать список в многомерный массив?

1 Ответ

1 голос
/ 30 апреля 2019

Используйте словарь:

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