8
Answers

How to convert one list to multidimensional array?

BKS Saha

BKS Saha

7y
206
1
  1. public JsonResult AllListofMainDivisionDesignation() {       
  2. List<HrDesignation> designation = new List<HrDesignation>();   
  3.      var designationgroup = db.HrDesignationGroup.Where(p=>p.Rank==2).ToList();   
  4.      foreach (var des in designationgroup)   
  5.     {         var desig = db.HrDesignation.Where(i=> i.DesignationGroupId == des.DesignationGroupId).OrderBy(r => r.Rank).ToList();  
  6.  designation.AddRange(desig);}  
  7.  HrDesignation[][] arrays = designation.Select(a =>a.ToArray()).ToArray();    
  8.  return Json(arrays, JsonRequestBehavior.AllowGet);   
  9.  }  
Answers (8)
1
Nikunj Satasiya

Nikunj Satasiya

NA 1.5k 144.6k 7y
Ok Don't warry we will try another way. i will create demo for you and sand you new code soon as possible.
0
BKS Saha

BKS Saha

NA 32 805 7y
Thank you sir
Nikunj Satasiya
0
BKS Saha

BKS Saha

NA 32 805 7y
Nikunj Satasiya sir,
I have already used similar things....but it is not working
0
Nikunj Satasiya

Nikunj Satasiya

NA 1.5k 144.6k 7y
I can see there are 3 solution for it .
 
1. Try this 
  1. HrDesignation[][] arrays = designation.Select(a =>a.ToArray());      
 instad of
  1. HrDesignation[][] arrays = designation.Select(a =>a.ToArray()).ToArray();      
2. add namespace
  1. using System.Linq;  
3. change name of array any try again. this may help full to you.
 
try every case one by one. i hope it will work. 
 
0
BKS Saha

BKS Saha

NA 32 805 7y
Dear Nikunj Satasiya Sir,
 
'HrDesignation' does not contain a definition for 'ToArray' and no extension method 'ToArray' accepting a first argument of type 'HrDesignation' could be found (are you missing a using directive or an assembly reference?)
0
Nikunj Satasiya

Nikunj Satasiya

NA 1.5k 144.6k 7y
can You Show me Actuall Error u getting .
0
BKS Saha

BKS Saha

NA 32 805 7y
Thank you sir
 
Nikunj Satasiya
 
I am facing problem this line.
 
HrDesignation[][] arrays = designation.Select(a =>a.ToArray()).ToArray();
0
Nikunj Satasiya

Nikunj Satasiya

NA 1.5k 144.6k 7y
Hi BKS Saha,
 
You can try this
  1. //extension method  
  2. public static object[,] To2DArray<T>(this IEnumerable<T> lines, params Func<T, object>[] lambdas)  
  3. {  
  4.   var array = new object[lines.Count(), lambdas.Count()];  
  5.   var lineCounter = 0;  
  6.   lines.ForEach(line =>  
  7.   {  
  8.     for (var i = 0; i < lambdas.Length; i++)  
  9.     {  
  10.       array[lineCounter, i] = lambdas[i](line);  
  11.     }  
  12.     lineCounter++;  
  13.   });  
  14.   return array;  
  15. }  
  16.    
  17. [Test]  
  18. public void Test()  
  19. {  
  20.   var lines = new List<Line>();  
  21.   lines.Add(new Line() { Id=1, Name="One", Age=25 });  
  22.   lines.Add(new Line() { Id = 2, Name = "Two", Age = 35 });  
  23.   lines.Add(new Line() { Id = 3, Name = "Three", Age = 45 });  
  24.    
  25.   //Convert to 2d array  
  26.   //[1,One,25]  
  27.   //[2,Two,35]  
  28.   //[3,Three,45]  
  29.   var range = lines.To2DArray(x => x.Id, x => x.Name, x=> x.Age);  
  30.    
  31.   //test the result  
  32.   for(var i=0;i<lines.Count;i++)  
  33.   {  
  34.     for(var j=0;j<3;j++)//3 lambdas passed to function  
  35.     {  
  36.       Console.Write(range[i,j]+",");  
  37.     }  
  38.     Console.WriteLine();  
  39.   }  
  40. }  
  41.    
  42. class Line  
  43. {  
  44.   public int Id { getset; }  
  45.   public string Name { getset; }  
  46.   public int Age { getset; }  
  47. }  
 OR Simpaly you cal also try this way.
  1. class Program  
  2. {  
  3.     static void Main()  
  4.     {  
  5.         // A.  
  6.         // New list here.  
  7.         List<string> l = new List<string>();  
  8.         l.Add("one");  
  9.         l.Add("two");  
  10.         l.Add("three");  
  11.         l.Add("four");  
  12.         l.Add("five");  
  13.   
  14.         // B.  
  15.         string[] s = l.ToArray();  
  16.     }  
  17. }