Filtering DataGrid, MVVM, Dynamic LINQ

c# datagrid dynamic-linq entity-framework-4 mvvm

Question

How can I filter my DataGrid items like MS Access filter works (just select by mouse some part of cell and press "Filter" button)?

I have ViewModel MyViewModel with 2 Commands (Filter and ClearFilter) and ObservableCollection I have Model with method GetItems(...);

in DataGrid CellEditEnding event I save selected path and column name:

_selectedColumn = e.Column;
if (e.EditingElement is TextBox)
    _selectedText = (e.EditingElement as TextBox).SelectedText;

So I have 2 params: my T object's property name, on which it is necessary to sort, and filter value (both are typeof String).

How should I change the Model's method GetItems, what would it work. Method Model.GetItemws uses EntityFramework to query items from database. How can I apply to this query my filter and how I can build this query? Some dynamic LINQ/Expression

var items = EFQuery().**Where(myFilter)**.ToList();

Where and how should I create this myFilter?

1
1
12/27/2010 8:11:48 AM

Popular Answer

You can write filter lambda like this:

.Where(x => x.GetType().GetProperty(AA).GetValue(x).ToString().Contains(BB))

Where AA is variable which contains selected column name

BB is a variable which contains a filter text.

Restriction: Your property AA must be the string or class which having overridden ToString Method

0
6/28/2019 12:31:58 PM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow