I have a LINQ query running with multiple joins and I want to pass it around as an IQueryable<T>
and apply additional filters in other methods.
The problem is that I can't work out how to pass around a var
data type and keep it strongly typed, and if I try to put it in my own class (EG: .Select((a,b) => new MyClass(a,b))
) I get errors when I try to add later Where
clauses because my class has no translations into SQL. Is there any way I can do one of the following:
- Make my class map to SQL?
- Make the
var
data-type implement an interface (So I can pass it round as though it's that)? - Something I haven't though of that'll solve my issue?
Example:
public void Main()
{
using (DBDataContext context = new DBDataContext())
{
var result = context.TableAs.Join(
context.TableBs,
a => a.BID,
b => b.ID,
(a,b) => new {A = a, B = b}
);
result = addNeedValue(result, 4);
}
}
private ???? addNeedValue(???? result, int value)
{
return result.Where(r => r.A.Value == value);
}
PS: I know in my example I can flatten out the function easily, but in the real thing it'd be an absolute mess if I tried.