question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: Reusable queries as extension methods?

See original GitHub issue

I 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:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
sdanylivcommented, Oct 27, 2020

@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.

public static IQueryable<pocoName> MyExtensionQuery(this myDb db, int id)
{
     return db.Table.Where(e => e.id == id);
}

var query = 
      from t in db.MyExtensionQuery(1)
      select t;

But there are cases when you need to reuse extension for more complex usage

var query = 
      from t1 in mydb.SomeTable
      from t2 in mydb.MyExtensionQuery(t1.Id)
      select t;

var query = 
      from t1 in mydb.SomeTable
      where mydb.MyExtensionQuery(t1.Id).Any()
      select t;

This case will be not convertible to the SQL. Here we have to instruct linq2db to correct Expression Tree by ExpressionMethodAttribute

[ExpressionMethod(nameof(MyExtensionQueryImpl))]
public static IQueryable<pocoName> MyExtensionQuery(this myDb db, int id)
{
     return db.Table.Where(e => e.id == id);
}

// Func types should be in the same order as in MyExtensionQuery and last type is result.
private static Expression<Func<myDb, int, IQueryable<pocoName>>> MyExtensionQueryImpl()
{
     return (db, id) => db.Table.Where(e => e.id == id);
}

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.

0reactions
4amycommented, Oct 27, 2020

Yes, got it working! Thank you.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found