[英]How can be dynamic this query of npoco
Instead of the adding search method to every class like this; 而不是像这样向每个类添加搜索方法;
public List<InvoiceDetail> SearchById(int Id)
{
return db.Query<InvoiceDetail>()
.Where(x => x.Id == Id)
.ToList();
}
How to add a method to base class like this; 像这样如何向基类添加方法;
public virtual List<T> SearchById(int Id)
{
return db.Query<T>()
.Where(x => x.Id == Id)
.ToList();
}
"T does not contain a definition for Id" “ T不包含ID的定义”
Because Id is the definition of Detail entities. 因为Id是Detail实体的定义。
You can achieve this by creating a base class as: 您可以通过创建一个基类来实现此目的:
public class BaseEntity
{
public int Id { get; set; }
}
Then make the appropriate entity classes inherit from BaseEntity
as: 然后,使适当的实体类从BaseEntity
继承为:
public class Detail : BaseEntity
{
//some props for Detail class only
}
Now for your search method you can use the where T : class
constrain as: 现在,对于您的搜索方法,您可以使用where T : class
约束为:
public List<T> SearchById<T>(int id) where T : BaseEntity
{
return db.Query<T>()
.Where(x => x.Id == Id)
.ToList();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.