score:0
I guess thinking more about it, it is somewhat of a stupid question. I was hoping to be able to use the cleaner query:
var query = from b in DataContext.B
select b;
And apply this to it:
x => x.A.Name == "Test"
Without having to have the duplicate this predicate that I use when starting the query on the A table:
x => x.Name == "Test"
So I suppose the solution is to "reverse" the query by starting on the A table, like so:
var query = from a in DataContext.A
join b in B on a equals b.A
select b;
query = query.Where(FilterPredicate());
I was thinking it may rewrite the queries inefficiently, but that doesn't seem to be the case.
score:3
You can do this by defining an interface over A and B.
public interface IHasName // contrived, I know
{
string Name {get;}
}
LINQ-To-SQL classes are partial, so in your part of the partial class definition, you can add the interface like so:
public partial class A : IHasName {}
public partial class B : IHasName {}
As you see, no implementation should be needed since the Name property is implemented in the Linq-To-Sql generated part.
Now constrain your predicate to types implementing the IHasName interface, and you're all set:
private Expression<Func<T, bool>> FilterPredicate(string name) where T : IHasName
{
return x => x.Name == name;
}
you should now even be able to define an extension method on IQueryable like so:
public static T GetByName<T>(this IQueryable<T> queryable,
string name) where T : IHasName
{
return queryable.Where(FilterPredicate(name)).SingleOrDefault();
}
Small caveat: of course, the property in the interface ('Name') must exactly match the property name in the implementing classes. Suppose you have a class C with property 'MyName'. You might be tempted to implement the IHasName interface like so:
public partial class C : IHasName
{
public string Name {return MyName;}
}
This will of course not work, as the Linq-To-Sql expression parser will use 'Name' instead of the actual property 'MyName', so it won't be able to map this expression to valid SQL.
Source: stackoverflow.com
Related Articles
- DRY LINQ Predicate Filters for Multiple Tables
- How to reuse a linq expression for 'Where' when using multiple source tables
- How to perform Join between multiple tables in LINQ lambda
- LINQ to SQL multiple tables left outer join
- Creating a LINQ select from multiple tables
- Group Multiple Tables in LINQ
- Linq group by multiple fields across different tables
- How to left join multiple tables with LINQ
- Multiple tables left join using Linq
- Does this LINQ code perform multiple lookups on the original data?
- LINQ to SQL: Complicated query with aggregate data for a report from multiple tables for an ordering system
- Linq - Group by multiple tables
- LinQ query with multiple tables and extracting data
- LINQ Source Code Available
- multiple orderby in this linq code
- Assign multiple tables to single class in linq table mapping
- LINQ to SQL join 3 tables and select multiple columns and also using Sum
- How to dynamic add filters to a LINQ query against an Odata Source in C#
- How to create linq predicate with multiple nested "ands" and "ors"
- multiple Search parameters in Multiple joined tables using LINQ in DBML
- Select data from multiple unrelated tables with LINQ to Entity Framework
- C# Linq Join 2 tables on multiple columns and GROUP BY for count
- Problem with simple join by multiple columns in two tables using linq
- Populate a C# class from a dataset with multiple linked tables using linq lambda
- Joining two tables and returning multiple records as one row using LINQ
- creating Linq to sqlite dbml from DbLinq source code
- Conditionally sorting elements by multiple properties in multiple tables with LINQ
- Pull data from multiple tables in one SQL query using LINQ and Entity Framework (Core)
- Entity Framework: How to perform left join with EF and LINQ among multiple tables
- Using LINQ to select all from multiple tables
- Trouble with LINQ Returning Results Set from Object[] Array Within Object
- select rows whose sum greater than 0 using Linq
- Why Does an Array Cast as IEnumerable Ignore Deferred Execution?
- Add a column to an IEnumerable in c# such that it works with WebGrid
- Convert Loop To Linq - Model Creation
- Does LINQ to Entities reuse instances of objects?
- Select columns dynamically from Datatable C#
- Entity Framework Nested Query Select() Problem
- Linq Nested Query Issue with decimal type
- LINQ .FromSQL error InvalidOperationException: Sequence contains more than one matching element
- How to perform operation on grouped records?
- Select Values from two tables using Linq
- The Linq Query Returns Errors
- LINQ join multi table
- LINQ and Nhibernate: create an Expression using a model property
- Lambda: using .Include() with linked table data in EF
- Insert a value to SQL Server database with LINQ on C#
- Can I access individual list item during LINQ Join?
- Converting Stored Proc to LINQ to Entities
- In IEnumerable extensions - why is only Count() optimized for ICollection?