I am working on data warehouse application and we have 4 tables where schema is identical. Only difference between those tables is just Table Name.
Table Example:
- ps_Contractor
- ps_Employee
- ps_Union
- ps_NonUnion
Schema
- id
- hourly
- benefit
- total
Now i need to generate 4 reports based on these tables. Instead of writing 4 separate LINQ queries i would like to write single query where i can pass the table name dynamically.
The question How do i pass the table name dynamically in following LINQ query ?
var data = ( from q in _dbcontext.ps_Contractor
join _l in _dbcontext.log on q.id equals l.tablelogid
where q.hourly = 8
select new{
hourly=q.hourly,
benefit=q.benefit,
total=q.total,
log = l.message
}.ToList();
I have looked at all similar questions suggested by stack overflow. I do not want to use ExecuteStoreQuery.
what options do i have ?
3条答案
按热度按时间wribegjk1#
如果所有表都有相同的列,那么我将从这些表中提取一个
interface
,并创建partial entity classes
来实现该接口,最后使用该接口进行查询。例如:
在search方法中,我会传递
IEnumerable<ICommonInterface>
或IQueryable<ICommonInterface>
,并在其上应用查询.您所需要做得只是将不同得表传递给该search方法.或者您甚至可以有ICommonInterface
类型得泛型类,并使用该类来执行查询.这只是我现在测试的一个示例:
实体:
测试项目:
在数据库中,有两个仅包含
Id
和Name
列的表8yoxcaq72#
EF Core不再有非泛型的.set方法,但这个扩展类使使用动态Linq基于字符串查询表变得容易
}
使用方法:
jm81lzqq3#
下面是一种执行动态函数的方法,该函数接受
DbSet<T>
(要作为参数传递的数据库类的类型)和特定表达式,以便在该表上构建查询:你可以这样调用这个函数