Why Linq2db Table class is sealed? Or how implement base filtering for generic tables #4150
-
Hello everyone, I need in TableT So, previously, using BlToolkit, we used Table and inherited from it... As example. We decided to port our Old Legacy Project to LinqToDb and want to implement the same behaviour So, for example, I generated ITables (ex. ITable for SomeEntity) from template as recommended Do you have any ideas how it possible to do? Thank you very much for any ideas. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I usually do it by adding top-level functions in my DB connection/context directly. Extensions methods, as well as generic methods, are other options, esp. if you want to filter multiple collections with the same code. // 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 though:
public IQueryable<T> Deleted<T>() where T: ISoftDelete
=> this.GetTable<T>().Where(x => x.IsDeleted);
// Use like this:
db.Deleted<SomeStuff>().First(x => x.Id == 42); Unless you want to change the built-in |
Beta Was this translation helpful? Give feedback.
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 havedb.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: