views:

19

answers:

2

Hi,

I have a list of items I am retrieving which i wish to be grouped into divs depending on the common name that a set of the list items may have

say for instance a list of firstnames I have.

i would like to be able to create a div dynamically based on the items common attibutes.

id 23 fistname darren id 37 fistname darren id 67 fistname darren

id like to group all the firstnames darren into one div and any others that share common firstnames

cheers

A: 

I don't know about htmlhelper, but the linq groupby method is simple to use:

var firstnamegroups = items.GroupBy(item => item.FirstName);
Matt Ellen
A: 

I'll help you with the query only, you can continue after this point with the your HTML code.

Sample Data:

List<Person> list = new List<Person>();
list.Add(new Person() { FirstName = "A", ID = 1 });
list.Add(new Person() { FirstName = "B", ID = 2 });
list.Add(new Person() { FirstName = "B", ID = 3 });
list.Add(new Person() { FirstName = "C", ID = 4 });

The LINQ expression:

var result = list.GroupBy(item => item.FirstName);

Put here your HTML code:

foreach (var item in result)
{
    string name = item.Key;
    // Add here the div and use [name]

    foreach (var person in item)
        ;// Add here the items in the div
}

Good luck!

Homam