Reputation: 83
I had a ObservableCollection
(fields like username,firstname,lastname,password,address,contact no etc) which contains a huge set of rows, and also I am having a datatable (columns like SNo, username,status) which is having a limited number of rows only.
Now what I need to filter the ObservableCollection
based on the usernames present in the datatable in the datatable and bind the ObservableCollection
to datagrid using linq.
I done a sample linq Query it does not produce exact data.
var res = from a in Settings.GetInstance().ObservableClass
where dtStatusTable.Rows.Contains(a.UserName)
select a;
Settings.GetInstance().ObservableClass = res as ObservableCollection<IObservableClass >;
Note: UserName
is unique and may be contains similar but not exact. Like usernames may be like Manikandan, Mani, ManikandanSekar etc.
Kindly give me a solution to filter the data.
Upvotes: 0
Views: 2054
Reputation: 10800
Your current query says "where the datatable has a row of a.UserName", when it sounds like you want the query to say "where the datatable has a row that contains a.UserName in a specific column". You can make this a little clearer by doing something like this (not sure what your exact types are, so this is just some psuedo-query):
var usernamesInDataTable = dtStatusTable.Rows.Select(r => r.UserName);
var query = from a in ObservableClass
where userNamesInDataTable.Contains(a.UserName)
select a;
Upvotes: 1
Reputation: 69959
Perhaps you can do something like this which should select items with an exact match of username
:
YourObservableCollection = new ObservableCollection<YourDataType>(
from item in YourObservableCollection
join row in YourDataTable on item.username equals row.username
select item);
It's kind of tricky writing LinQ
queries without data and intellisense, so please forgive me if there are errors in it.
Upvotes: 0