0

I have a single list of data like

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

And now I want to create a multidimensional array according to that like public List<int>[][] arrayList{get;set;}

I need to convert list data to multidimensional array list like

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

I am trying to applying this but it's showing me error for Invalid Expression. So How can I convert list into multidimensional array ?

5
  • 1
    List<int>[][] this is not a multidimensional array, its a jagged array of List<int> Commented Apr 30, 2019 at 9:40
  • Aside from what TheGeneral said, ... = [something, something, something]; is not even valid C# syntax. I guess it perhaps indicates the need to spend some more time with C# tutorials covering the basics of how to create, initialize and use arrays and collections.... Commented Apr 30, 2019 at 9:45
  • I am trying to List<int>[][] array = new int[][] {item.Key, item1.VillageId, item1.TownId }; but it's also not converting it. Commented Apr 30, 2019 at 9:49
  • You declare a variable of type List<int>[][] but then try to create and assign a int[][] instance to it. (An int[][] is not even remotely the same as nor is it convertible to List<int>[][]) What? That doesn't make a lick of sense... Commented Apr 30, 2019 at 9:51
  • @elgonzo, how can I add simple int[][] to List<int>[][] ? Commented Apr 30, 2019 at 11:04

1 Answer 1

1

Use a Dictionary :

   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 {;}
        }
    }
Sign up to request clarification or add additional context in comments.

3 Comments

Dictionary is new for me. can I able to convert this Dictionary into List<int>[][] ?
You really have three levels of arrays (not two) since you are using a GroupBy. A GroupBy creates a two dimensional array [key][List]. In your case the List is an array of attributes (BlockId, DistrictId, TownId, ...). So do you really want to remove the attribute names like "BlockId" when you say List<int>[][]?
Yes actually I need List of Int which will contains two array in which first one will contains villageId and second one will contains townId. I am searching for convert dictionary to List<int>[][] but almost are using only int[][].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.