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 Query
- DRY LINQ Predicate Filters for Multiple Tables
- How to reuse a linq expression for 'Where' when using multiple source tables
- LINQ to SQL: Complicated query with aggregate data for a report from multiple tables for an ordering system
- C# Linq Join 2 tables on multiple columns and GROUP BY for count
- LINQ help for a beginner selecting multiple tables
- ASP.NET LINQ query for filter and loop through multiple tables
- How to assign multiple LINQ Include() statements to a variable for code re-use?
- source code for LINQ 101 samples
- LINQ Query for fetching data from Multiple Tables
- Linq query for multiple join tables in C#
- LINQ for an SQL with multiple LEFT OUTER JOINS on different tables
- Asp.net mvc Linq query for multiple filters
- Building a Linq Query for multiple Foreign tables
- How to use LINQ for multiple filters inside a loop?
- How to use Left join linq for multiple tables
- LINQ statement for joining multiple tables (Entity Framework 6)
- 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
- Select All columns for all tables in join + linq join
- LINQ Single() Exception for 0 or multiple items
- Linq query with multiple Contains/Any for RavenDB
- LINQ to SQL - How to efficiently do either an AND or an OR search for multiple criteria
- 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?
- Suggestions for designing complex LINQ code
- Linq - Group by multiple tables
More Query from same tag
- How do I parent-child pairs efficiently?
- Insert fk at the same time as attributes are added
- How to find duplicate items in list<>?
- Is Linq2XSD Dead?
- How to set a DTO's property value directly in LINQ select statement
- How to detach a LINQ-to-SQL data object from the DataContext's tracking mechanism?
- linq to xml query with all attributes
- Linq: adding conditions to the where clause conditionally
- string.Join() doesn't work as expected
- An issue with LINQ Group by query
- How to group by specific attributes and orderby another one?
- How/Can I use linq to xml to query huge xml files with reasonable memory consumption?
- How can I get a list of DirectoryInfo where the name of the directories contains a string stored in a List<string>?
- Populate StackPanel with checkbox's from Database
- Comparing two strings containing multiple dots inside a Linq to Sql query
- Remove ONLY the childrens with LINQ to SQL?
- C# Linq - SET syntax from SQL, i.e. SET abc (a property of obj) WHERE xyz = true in IEnumerable<obj>
- Linq using how to use order by and group by with left join?
- Group a list of url strings based on folder structure
- DataServiceVersion conflict: The DataServiceVersion '1.0' is too low for the request. The lowest supported version is '3.0'
- Basic Linq and order by
- How to use Linq to check if a string contains one of a subset of strings
- Linq query where First() appears to return null when results are not found
- Entity Framework Linq embed lambda expressions into a reusable function
- Can 2 Linq queries be passed to 1 View?
- C# / .NET comparing two large lists and finding missing items from both list
- How to append an array to a List using LINQ?
- Not able to select values from right list using LINQ join
- How to set up multiple IObservers in a loop
- stating which conditions have been matched in linq where