views:

46

answers:

1

Hi!

In my WPF application I'd like to use LINQ as much as possible (expecially to avoid the "for each"). But WPF works a lot with the ObservableCollection, and I can't use LINQ with these kind of collection. How can I do?

Thanks, Pileggi

+2  A: 

What makes you think you can't use LINQ with ObservableCollection<T>? It implements Collection<T> so it should be fine.

For example:

using System;
using System.Collections.ObjectModel;
using System.Linq;

class Test
{
    static void Main()
    {
        var collection = new ObservableCollection<int>()
        {
            1, 2, 3, 6, 8, 2, 4, 5, 3
        };

        var query = collection.Where(x => x % 2 == 0);
        foreach (int x in query)
        {
            Console.WriteLine(x);
        }
    }
}
Jon Skeet
Thank you. Sorry. I remember there were problems with some queries, but now I have tried and everything works.
pileggi