Skip to content

Why Linq2db Table class is sealed? Or how implement base filtering for generic tables #4150

Closed Answered by jods4
DimaVorobevDeveloper asked this question in Q&A
Discussion options

You must be logged in to vote

I usually do it by adding top-level functions in my DB connection/context directly.
Instead of db.SomeEntities.GetActualItems().Where(x => x.Id == 1) I would have db.GetActualItems().Where(x => x.Id == 1).

Extensions methods, as well as generic methods, are other options, esp. if you want to filter multiple collections with the same code.
For example if you have a soft-delete flag you could do:

// Extension method:
public static IQueryable<T> DeletedItems<T>(this IQueryable<T> src) where T: ISoftDelete
  => src.Where(x => x.IsDeleted);
  
// Then you can do:
db.SomeStuff.DeletedItems().First(x => x.Id == 42);

// You could do the same with a generic method on DataConnection/DataContext th…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by DimaVorobevDeveloper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants