Question: Reusable queries as extension methods?
See original GitHub issueI am looking for an example of how I can write extension methods with parameters that have access to the full db context similar to what’s shown on the CTE page. Does anyone have examples of how to do that so I can make calls such as
db.MyExtensionQuery(1)
or
from m in db.MyExtensionQuery
or
from sometable in db.sometables
join myQuery in db.MyExtensionQuery on sometable.id equals myQuery.sometableId
I tried writing it like:
public static IQueryable<pocoName> MyExtensionQuery(this myDb db, int id)
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Reusable Querying in Entity Framework WITHOUT ...
This is a blog post I wrote about building reusable queries. Using Extension Methods allows you to build composable queries.
Read more >How do you reuse queries in .NET Core with Entity ...
Extension methods are a good option for reusing queries, and also they are easy to implement and change. Do not overcomplicate for basic ......
Read more >Giving Clarity to LINQ Queries by Extending Expressions
LINQ expressions can be made much easier to comprehend and modify by using extension methods to create pipes and filters.
Read more >Extension Methods - C# Programming Guide
The most common extension methods are the LINQ standard query operators that add query functionality to the existing System.Collections.
Read more >3 Ways to Refactor EF Linq Queries w/o Killing Performance
Here are three easy solutions including: Expressions, Extension Methods, and LinqKit. Lee Richardson. Sep 6, 2019 • 4 min read.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@4amy, you can create any extension methods over DataConnection or IDataContext or IQueryable, with limitation that parameters that are passed to these extensions will be not fields of other tables.
But there are cases when you need to reuse extension for more complex usage
This case will be not convertible to the SQL. Here we have to instruct linq2db to correct Expression Tree by
ExpressionMethodAttribute
After these manipulations all queries described above become valid.
Also check my answer https://stackoverflow.com/questions/63727166/linq2db-filter-by-nested-property-field for another scenario and this blog post http://blog.linq2db.com/2016/06/how-to-teach-linq-to-db-convert-custom.html.
Yes, got it working! Thank you.