Let the following take place:
public interface IOne { UInt64 Id { get; } Int16 Size { get; } } public interface ITwo { UInt64 OneId { get; } Int16 Color { get; } }
As explained here , the way to reuse a linq expression is to write something like this:
public static Expression<Func<IOne, bool>> MyWhereExpression( int value ){ return (o) => (o.Size > value); } int v = 5; IQueryable<IOne> records = from one in s.Query<IOne>() .Where(MyWhereExpression(v)) select one;
When I want to do the same with two tables, I run into a problem.
Expression:
public static Expression<Func<IOne, ITwo, bool>> MyWhereExpression2(int color ) { return (one,two) => (one.Id == two.OneId) && (two.Color > color ); }
Linq 1:
int color = 100; IQueryable<IOne> records = from one in s.Query<IOne>() from two in s.Query<ITwo>() .Where(MyWhereExpression2(color)) select one;
This does not work. If you use only 2nd.
Linq 2:
int color = 100; IQueryable<IOne> records = (from one in s.Query<IOne>() from two in s.Query<ITwo>() select new { one, two }) .Where(MyWhereExpression2(color));
The result is
Argument 2: cannot be converted from 'Expression <System.Func <IOne, ITwo, bool β' to 'System.Func <AnonymousType # 1, int, bool>'
I understand the error message in AnonymousType, but I cannot figure out how to write the request.
The reason I want to use an expression and not just write
where (one.Id == two.OneId) && (two.Color > color )
directly in the linq query is that I want to reuse this expression in multiple linq queries.