WPF LINQ and ObservableCollection

In my WPF application, I would like to use LINQ as much as possible (especially to avoid foreach). But WPF works a lot with ObservableCollection, and I cannot use LINQ with such a collection. What can I do?

+5
source share
3 answers

Why do you think you cannot use LINQ with ObservableCollection<T>? He implements Collection<T>, so everything should be fine.

For instance:

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);
        }
    }
}
+8
source

Just for those who might run into this problem, trying to filter out the ObservableCollection, but finding that they can't.

, , , , WPF , , " System.Linq;" . , ".where" .

+13

The OP specifically requested the LINQ method ".ForEach ()", which cannot be used in ObservableCollection <T>, because it is implemented for List <T>.

There is another SO-Topic where I found my solution: fooobar.com/questions/6293 / ...

0
source

All Articles