Sign up ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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<Assignment> 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<Assignment> assignmentList)
{
    ListViewGroup _listViewGrp;
    ListViewItem _listViewItem;

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

    for (int i = 0; i < 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();
}
share|improve this question

1 Answer 1

up vote 2 down vote accepted

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<Assignment> assignmentList)
{
    MonitorPane.BeginUpdate();
    MonitorPane.Groups.Clear();
    MonitorPane.SmallImageList = imageList;

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

    MonitorPane.EndUpdate();
}

private static ListViewGroup CreateListViewGroup(TimeSpan time, IEnumerable<Assignment> 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;
}
share|improve this answer
    
Hi George, When I add this code of yours, the CreateListViewGroup is not getting called. –  Govs Jun 2 '14 at 13:50
    
@Govs Forgot that GroupBy is deferred. Fixed. –  George Howarth Jun 2 '14 at 13:58
    
Downvoter, care to explain what is horrendously wrong with the answer? –  George Howarth Jun 2 '14 at 14:00
    
Cool, That works like a charm. Clean. I am going to accept this as answer. Who ever down voted please care to explain if you have a better answer. –  Govs Jun 2 '14 at 14:07
    
Hi George, I searched online and read through some posts and blogs about GroupBy method. But I still don't fully understand how it works. I understand that it groups by time and then pass each group to the CreateListViewGroup method. But the method returns group back. How and where does this returned group gets handled? Do you have any link which can explain this? –  Govs Jun 2 '14 at 15:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.