3

My C# class is as follows

public class SeatPlans
{
    public int Id { get; set; }
    public string seat_id { get; set; }
    public string seat_no { get; set; }
    public int layout_id { get; set; }
}

I have created a list of class object as follows

List<SeatPlans> allUser = new List<SeatPlans>();

but what I need is an array of lists so that I can access the data of several objects like allUser[0], allUser[1], allUser[2]

My controller code is as follows

public JsonResult getdata(int seat_plane_id)
{
    int lid = seat_plane_id;
    layoutsController L = new layoutsController();
    JsonResult result = L.getlayouts(lid);

    List<layouts> L1 = (List<layouts>)result.Data;
    List<List<SeatPlans>> allUser = new List<List<SeatPlans>>();

    for (int i = 0; i < L1.Count; i++)
    {
        String lid1 = L1[i].ticket_no_start;
        lid = Int32.Parse(lid1);
        allUser[i] = db.SEATPLAN.Where(d => d.layout_id == lid).ToList();
    }

    var v = new { allUser = allUser[0], allUser1 = allUser[1] };

    return Json(v, JsonRequestBehavior.AllowGet);
} 

I'm getting

System.ArgumentOutOfRangeException exception

at

allUser[i] = db.SEATPLAN.Where(d => d.layout_id == lid).ToList();
7
  • You can still access the list item by index using allUser[0]. So getting the Id would be allUser[0].Id Commented May 14, 2016 at 7:26
  • allUser[0] contain the value of Id right?,but actualy i nee a separate list of classes in allUser[0],allUser[1],allUser[2] Commented May 14, 2016 at 7:29
  • 1
    Yes, you can access all the public properties this way. Commented May 14, 2016 at 7:30
  • 2
    i need something like this --- allUser[0] = db.SEATPLAN.Where(d => d.layout_id == lid).ToList(); allUser[1] = db.SEATPLAN.Where(d => d.layout_id == lid).ToList(); Commented May 14, 2016 at 7:43
  • ToList() returns a list. Trying to set to allUser[0] doesn't make sense unless you really want List<List<SeatPlans>>. You may need to use a GroupBy to take the List<SeatPlans> and break into groups so you would have a List<List<SeatPlans>> Commented May 14, 2016 at 7:48

3 Answers 3

2

To have an array of lists you can simply declare it:

List<SeatPlans>[] allUsers = new List<SetPlans>[size];

then you can get specific list by index:

allUsers[1] = new List<SetPlans>();

to get specific element of specific list:

SeatPlans specificSeatPlans = allUsers[1][2];
Sign up to request clarification or add additional context in comments.

Comments

1

you can use allUser[0],allUser[1],allUser[2] with List as well. allUser[0] gets the first SeatPlan object then you can access public members from that.

SeatPlan sp = allUser[0];
Console.Writeline(sp.Id);

will print out the first SeatPlan object's Id.

3 Comments

if i use the allUser[0] instead allUser it shows error(red marks)
@rakshi can you post your code where you are trying to use it.
sir i have posted my controller
1

If you need an 2d array of objects you can create it like this:

List<List<SeatPlan>> allUser = new List<List<SeatPlan>>();

allUser.Add(db.SEATPLAN.Where(d => d.layout_id == lid).ToList());

note, you need to use the Add method to push a new object onto the list.

Accessing the individual objects can then be done via allUser[0][0], getting the Id for example would be allUser[0][0].Id

2 Comments

i am getting System.ArgumentOutOfRangeException from the point--- allUser[0] = db.SEATPLAN.Where(d => d.layout_id == lid).ToList();
You can't initialize a list that way. The constructor creates a 0 size List so allUser[0] doesn't exist yet. You have you use the Add method like in my answer.

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.