Adding groups to ListView depending on the data in a list I have written simple code which loops through a list object and create groups in ListView depending on the data in the list. I have a list object of type List&lt;Assignment&gt; where Assignment is a class as follows.

public class Assignment
{
    public TimeSpan Time { get; set; }
    public string Type { get; set; }
    public string Description { get; set; }
    public int ImageIndex { get; set; }
}


Now, the list object have several assignments with different TimeSpan. My job is to group together all the Assignments with similar TimeSpan into one group and add this group to the ListView. The following code works perfectly fine for me. Still, I would love to know if there is better way of doing this.

The list passed into the function is sorted by TimeSpan.

private void FeedDataToPane(List&lt;Assignment&gt; assignmentList)
{
    ListViewGroup _listViewGrp;
    ListViewItem _listViewItem;

    MonitorPane.BeginUpdate();
    MonitorPane.Groups.Clear();
    MonitorPane.SmallImageList = imageList;

    for (int i = 0; i &lt; assignmentList.Count; i++)
    {
        if (i == 0)
        {
            _listViewGrp = new ListViewGroup(assignmentList[i].Time.ToString());
            MonitorPane.Groups.Add(_listViewGrp);

            _listViewItem = new ListViewItem(assignmentList[i].Type, assignmentList[i].ImageIndex, _listViewGrp);
            _listViewItem.SubItems.Add(assignmentList[i].Description);
            MonitorPane.Items.Add(_listViewItem);
        }
        else
        {
            if (assignmentList[i].Time.Equals(assignmentList[i - 1].Time))
            {
                _listViewItem = new ListViewItem(assignmentList[i].Type, assignmentList[i].ImageIndex, _listViewGrp);
                _listViewItem.SubItems.Add(assignmentList[i].Description);
                MonitorPane.Items.Add(_listViewItem);
            }
            else
            {
                _listViewGrp = new ListViewGroup(assignmentList[i].Time.ToString());
                MonitorPane.Groups.Add(_listViewGrp);

                _listViewItem = new ListViewItem(assignmentList[i].Type, assignmentList[i].ImageIndex, _listViewGrp);
                _listViewItem.SubItems.Add(assignmentList[i].Description);
                MonitorPane.Items.Add(_listViewItem);
            }
        }
    }

    MonitorPane.EndUpdate();
}
It's possible with the help of LINQ to reduce your code to this (assuming, of course, the version of .NET you're using will allow it):

private void FeedDataToPane(IEnumerable&lt;Assignment&gt; assignmentList)
{
    MonitorPane.BeginUpdate();
    MonitorPane.Groups.Clear();
    MonitorPane.SmallImageList = imageList;

    assignmentList
        .GroupBy(assignment =&gt; assignment.Time, CreateListViewGroup)
        .ToList();

    MonitorPane.EndUpdate();
}

private static ListViewGroup CreateListViewGroup(TimeSpan time, IEnumerable&lt;Assignment&gt; assignments)
{
    var group = new ListViewGroup(time.ToString());
    MonitorPane.Groups.Add(group);

    foreach (var assignment in assignments)
    {
        var item = new ListViewItem(assignment.Type, assignment.ImageIndex, group);
        item.SubItems.Add(assignment.Description);
        MonitorPane.Items.Add(item);
    }

    return group;
}