Reputation: 3
I have a requirement to create a linq query that selects data from a database table where a certain functional result is true.
I am using linq-to-sql
and the function works well and I can write a basic query to get the data.
My issue is that I have an in-memory list of parameters and I essentially need to run the linq query multiple times (once for every parameter list item) and aggregate the results.
I have tried using the .Any()
as a join but linq doesn't like joining non-database result sets with database result sets.
Some Sample Code:
Parameter list: // lets call it "l"
{
One,
Two,
Three
}
Query
From w in words where funcWord(l.item) == true select w;
So I would require a query that can run the above query once for every item in l and aggregate the results.
Any help is appreciated.
Thanks.
Upvotes: 0
Views: 1111
Reputation: 2531
Multiple from
statements will go through every combination, sort of like nested foreach
statements:
from item in list
from w in words
where funcWord(item, w.name) == true
select w;
Interestingly, this gets translated to a SelectMany
statement that is similar to Arithmomaniac's answer.
Upvotes: 1
Reputation: 4804
Try SelectMany, which aggregates the result of a one-to-many function, applied to each member.
In this case, the members are the elements of list
, and the one-to-many function is your above, l
-dependent query (though I rewrote it as a lambda function.)
list.SelectMany(l => words.Where(w => funcWord(l.item, w.name)));
Upvotes: 0