The result of a query expression is a query, not the result of the query

This bit me recently. To quote Eric Lippert (last paragraph):

Remember that with LINQ the result of a query expression is a query that can deliver the results when asked, not the results of the query.

This code will result in the PropertyChanged event handler never being called:

    var items = (from item in MethodThatGetsItemsAsEnumerable()
                select new ViewModelEntity(item));

    foreach (var item in items)
    {
        item.PropertyChanged += item_PropertyChanged;
    }

    this.Items = new ObservableCollection<ViewModelEntity>(items);

The correct code is, of course:

    var items = (from item in MethodThatGetsItemsAsEnumerable()
                select new ViewModelEntity(item)).ToList();

    foreach (var item in items)
    {
        item.PropertyChanged += item_PropertyChanged;
    }

    this.Items = new ObservableCollection<ViewModelEntity>(items);

The first code will result in MethodThatGetsItemsAsEnumerable being called twice, once when items is iterated over in the foreach and once when the ObservableCollection is constructed, ergo the item's that had item_PropertyChanged attached have long since disappeared. The ToList() in the second version is the solution as it turns the query into the results of the query.

About Rob

I've been interested in computing since the day my Dad purchased his first business PC (an Amstrad PC 1640 for anyone interested) which introduced me to MS-DOS batch programming and BASIC.

My skillset has matured somewhat since then, which you'll probably see from the posts here. You can read a bit more about me on the about page of the site, or check out some of the other posts on my areas of interest.

No Comments

Add a Comment