Filtering a collection of items from contents of another collection?

How about something like: foreach(string s in filters) { if(s. Length == 0) continue; string tmp = s; // because of "capture" problem index = index. Where(i => i.FilterNames.

Contains(tmp)); } return index This applies a succession of Where clauses, covering all the filters - essentially AND.

How about something like: foreach(string s in filters) { if(s. Length == 0) continue; string tmp = s; // because of "capture" problem index = index. Where(i => i.FilterNames.

Contains(tmp)); } return index; This applies a succession of Where clauses, covering all the filters - essentially AND.

– zsharp Apr 2 '09 at 23:18 @zsharp the lambda inside of Where() is a closure. The variable s for that iteration must be kept otherwise it'll be different by the time Where lambda is executed. See closures.

– CVertex Apr 3 '09 at 3:57 Because we use "s" in a lambda, it is actually treated (by the compiler) as a field on a compiler-generated class (for complex reasons). The problem is that the nature of "foreach" means that otherwise you'd only get one of them, and you'd have n times Where(last filter). – Marc Gravell?

Apr 3 '09 at 5:44.

Straight forward. For a given item from index check that it is true for all filters that the given item contains the filter. With this just select all items from index for that the given condition is true.Index.

Where(item => filters. All(filter => item.FilterNames. Contains(filter))) I am not sure if the check for length greater than zero is required, nut it is easily integrated.Index.

Where(item => filters. All(filter => (filter. Length > 0 ) || (item.FilterNames.

Contains(filter)))) It works with LINQ to Objects and I guess it does what you want, but I am not sure if it works with LINQ to SQL.

Turn it around. What you want is those items in index where every item in FilterNames has a corresponding entry in filters. I'm not sure how performant it'd be, but a count comparison should do.

Something like: private IQueryable FilteredIndex(IQueryable index, IEnumerable filter) { var filteredIndex = from I in index where (from s in i. FilterNames where filter. Contains(s) select s).Count() == i.FilterNames.

Count select i; return filteredIndex; }.

With this just select all items from index for that the given condition is true. I am not sure if the check for length greater than zero is required, nut it is easily integrated. It works with LINQ to Objects and I guess it does what you want, but I am not sure if it works with LINQ to SQL.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions