0

I have database and I'm using Entity Framework to create models (database first). I want to loop somehow through my model so that I can create a list of each row. Then I want to create list of those lists. Also lists have to be lists of objects not strings.

Model class:

public class person
{
    public int ID {get; set;}
    public string Code {get; set;}
    public int Time {get; set;}
    public string Name {get; set;}
}

Now I have such a person in the database and I want to create a list that contains ID, Code, Time and Name taken from one row. And then I want to create a list of those Person lists. I assume that I should use foreach or for loop but I don't know how should I start.

Edit: I have database that looks like this:

ID | Code | Time | Name
 1    200    10    Paul
 2    201    20    Tom
 3    203    25    Jacob
 4    208    10    Agnes

Now i want to create a list with one row. That list should look like:

List<1,200,10,Paul>

Then i want to create or rather add to list of lists each list created from those rows. So that i would have list that contains lists of Persons.

7
  • 2
    Your question is unclear. If you want a list of people your can get that with something like context.Persons.ToList(); This will return a List<Person>. You would use foreach in your view to loop through that list. Commented Sep 18, 2017 at 22:25
  • I need that list of lists in controller to do other things with it. Commented Sep 19, 2017 at 15:27
  • Still not sure what "list of lists" means. You have only shown a person model with no relationships so all you can really get is a List<Person>. It seems to me you are using MVC. So in your controller GET you would fill a list of persons from the context as shown. Then you would pass and display this in a View with foreach. More info needed... Commented Sep 19, 2017 at 16:33
  • I don't need to pass it to view. I want to create List<List<Person>>. Commented Sep 19, 2017 at 16:46
  • Your question is very unclear. If you have List<List<Person>>, what is inside each List<Person> that separates it from the others? Commented Sep 19, 2017 at 17:45

3 Answers 3

1

I can think of no legitimate reason to do this, but perhaps this is what you mean?

var listOfLists = db.Persons.Select(p => new List<object> {
    (object)p.ID,
    (object)p.Code,
    (object)p.Time,
    (object)p.Name
}).ToList();
Sign up to request clarification or add additional context in comments.

2 Comments

But what does it create? List of all ids, list of all Codes etc?
This creates a List<object> from each row, and then creates a List<List<object>> from those, which is what your question states. If that isn't what is desired, I would improve your question, including adding examples.
0

If you want to just go through list of Person's data item you need just foreach:

foreach(var data in ctx.Persons.Select( p => new {p.ID, p.Code, p.Time, p. Name})){
    // here you have data object with 4 items
}

2 Comments

Unfortunately i don't want anonymous types. Can i specify types of these 4 objects?
The four objects have types. You can specify a class instead of having an anonymous object, but then you are back to just using List<Person>.
0

Here is one way to create a <List<List<Person>>:

[HttpGet]
public ActionResult PersonIndex()
{
    List<List<Person>> personLists = new List<List<Person>>();
    var personsCodeA = context.Persons.Where(p => p.Code == "A").ToList();
    var personsCodeB = context.Persons.Where(p => p.Code == "B").ToList();
    var personsCodeX = context.Persons.Where(p => p.Code == "X").ToList();
    personLists.Add(personsCodeA);
    personLists.Add(personsCodeB);
    personLists.Add(personsCodeX);
    return View(personLists);
}

Now in the view (or where ever) you could do:

foreach (var currentList in personLists)
{
    // process each list
    console.writeline(string.Format("People in code {0}:",currentList[0].Code));  //todo null check
    foreach (var person in currentList])
    {
        // process each person in the current list
        console.writeline(string.Format("  Person: {0}",person.Name));  //todo null check
        var personID = person.ID;
        var personCode = person.Code;
        var personName = person.Name;
        var personTime = person.Time;
    }
}

Comments

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.