score:1
answering my own question...
after some experiments (including nick guerrera's original answer) i took another approach - instead of trying to reuse the expression i reuse the whole linq. however, it still required creating a container struct.
struct pair {
public ione one { get; set; }
public itwo two { get; set; }
public pair(ione one, itwo two) : this() {
one = one;
two = two;
}
}
public iqueryable<pair> get(isession s, int color) {
return from one in s.query<ione>()
from two in s.query<itwo>()
where (one.id == two.oneid) && (two.color > color)
select new pair(one, two);
}
now i can call
get(s, color).count();
and
var records = (from data in get(s, color) select data).take(2000);
etc.
score:1
first of all why do you have two tables with same set of fields? ideally database design should avoid same field names in different tables. design should use inheritance. common fields should move into base class and ef let's you create table per hierarchy, that should resolve your problem.
if you create your model with table per hierarchy then you will not need interface, and your linq queries can use shared filter expressions.
there is no way to achieve what you are asking unless you sit down and write a complex reflection based method that will clone your expression from one type to another type.
score:3
there may be a more elegant solution that escapes me at the moment, but you could just use tuple<ione, itwo>
instead of the anonymous type:
static expression<func<tuple<ione, itwo>, bool>> mywhereexpression2(int color) {
return t => (t.item1.id == t.item2.oneid) && (t.item2.color > color);
}
int color = 100;
iqueryable<ione> records = (from one in s.query<ione>()
from two in s.query<itwo>()
select tuple.create(one, two))
.where(mywhereexpression2(color))
.select(t => t.item1);
update: i probably answered too quickly above as that won't work with linq to sql since the call to tuple.create cannot be translated to sql. to work with linq to sql, the only solution i see at the moment is to create a named type:
class pair
{
public ione one { get; set; }
public itwo two { get; set; }
}
static expression<func<pair, bool>> mywhereexpression2(int color) {
return p => (p.one.id == p.two.oneid) && (p.two.color > color);
}
int color = 100;
iqueryable<ione> records = (from one in s.query<ione>()
from two in s.query<itwo>()
select new pair { one = one, two = two })
.where(mywhereexpression2(color))
.select(p => p.one);
Source: stackoverflow.com
Related Query
- How to reuse a linq expression for 'Where' when using multiple source tables
- How can I create an Lambda Expression for Linq in the Where clause for two tables after the Join?
- How to skip some conditions when using multiple where in linq
- How to Update multiple tables with foreign keys using LINQ expression ASP.NET MVC Entity Framework
- When using a LINQ Where clause on a Dictionary, how can I return a dictionary of the same type?
- Using LINQ with MVC, how can I include multiple parameters into a where clause?
- Using LINQ with a WHERE to ask for multiple possible results without redundant "||" commands
- LINQ to SQL bug (or very strange feature) when using IQueryable, foreach, and multiple Where
- Joining and grouping multiple tables with LINQ when using a linking table
- How can I check the number of calls to the database in LINQ query when using .NET Core and Code First?
- How do I prevent multiple results when condition is null for a LINQ query?
- How to create a LINQ To SQL datacontext for large DB using SQLMetal when it generates a 12MB DataContext.cs file?
- How to assign multiple LINQ Include() statements to a variable for code re-use?
- How to query multiple tables using LINQ syntax in .NET core 3
- How to check for null on Nullable types using Linq expression in Realm?
- How to sum values for multiple columns in DataTable using Groupby with Linq
- How to return multiple rows using LINQ lambda expression from SQL Server 2014?
- How to make types match in LINQ to Entities when using .GroupJoin extension with multiple conditions?
- How to JOIN two data tables using LINQ and Where condition?
- How to display the data of two tables in a view using LINQ lambda expression in ASP.Net MVC?
- Creating hierarchical JSON result from multiple tables using linq expression
- Getting null reference error in linq expression when trying to join more than 2 tables using defaultifempty method
- how to generate where condition without multiple if condition using linq query
- How can I create a LINQ expression that joins four tables and allows a where on the topmost table?
- How to Get results from multiple tables using LINQ
- How to select multiple tables using LINQ
- how to join multiple tables in linq using flag field
- How to execute an update on multiple rows using Linq Lambda Expression in ASP.Net MVC?
- How to get Records from multiple tables using linq
- how to fetch data from database using linq query for relationship 1:N and N:N (between 3 entity) in asp.net mvc EF code first?
More Query from same tag
- Entity Data Model & DataGridView - Creating new objects
- Help with LINQ Expression
- Why does Class Coupling jump when I use LINQ?
- Is there a Linq operation to determine whether there are items in a collection who have the same values for a pair of properties?
- Controlling LINQ query order based on other list of integers
- Invalid Column Name Entity guessing a foreign key that isn't there
- Oracle Error - Cast is not valid
- TSQL equivalent of Linq SelectMany
- Accessing columns in cross reference table via LINQ with Entity Framework
- Using Menu Buttons to modify LINQ search terms
- Retrieve All Children From Parent[]
- Split - Join, Aggregate, Zip, LINQ
- Unique object counts are showing system text
- Linq method for creating a sequence of separate objects?
- Remove KeyValue from ObservableCollection most effective way?
- Why does my string not parse properly in my linq to objects query?
- Blazor, LINQ ORDER BY of String with Quarter and Year
- Findcontrol in listview itemtemplate
- How to do Multiple Left Join, Group By, and Concatenate in LINQ
- Select Top for Subquery with Entity Framework
- Add node and elements in xml using C#/Linq
- How can I achieve to implement the below query without the if statement (SUM/AVG)?
- Linq query Join objects with different data sources?
- Entity Framework multiple relationships between tables
- Linq to Group By
- Linq union all equivalent of sql code
- WCF Data Services, DTO, and Entity Framework: The argument to DbIsNullExpression must refer to a primitive, enumeration or reference type
- LINQ - Deserialize JSON column and filter
- Conversion of a LINQ query fom method syntax to query syntax
- How can a build an XML tree in VB.NET reading the element names from a text file?