c# - Dynamically set the table name in LINQ query -



c# - Dynamically set the table name in LINQ query -

i working on info warehouse application , have 4 tables schema identical. difference between tables table name.

table example:

ps_contractor ps_employee ps_union ps_nonunion

schema

id hourly benefit total

now need generate 4 reports based on these tables. instead of writing 4 separate linq queries write single query can pass table name dynamically.

the question how pass table name dynamically in next linq query ?

var info = ( q in _dbcontext.ps_contractor bring together _l in _dbcontext.log on q.id equals l.tablelogid q.hourly = 8 select new{ hourly=q.hourly, benefit=q.benefit, total=q.total, log = l.message }.tolist();

i have looked @ similar questions suggested stack overflow. not want utilize executestorequery.

what options have ?

if tables have same columns, i'd extract interface out of tables , create partial entity classes implement interface, utilize interface query.

for example:

//entities public partial class ps_contractor: icommoninterface{} public partial class table2 : icommoninterface{}

in search method i'd pass ienumerable<icommoninterface> or iqueryable<icommoninterface> , apply query on that. you'd need pass different tables search method. or can have kind of generic class of type icommoninterface , utilize query.

public void example(iqueryable<icommoninterface>datasource) { var info = ( q in datasource bring together _l in _dbcontext.log on q.id equals l.tablelogid q.hourly = 8 select new{ hourly=q.hourly, benefit=q.benefit, total=q.total, log = l.message }.tolist(); } example(_dbcontext.ps_contractor.asqueryable())

this sample tested now:

public class repository { private list<string> getdata(iqueryable<icontractor> data) { homecoming (from d in info select d.name).tolist(); } public list<string> getfulltime() { using (var context = new testdbentities()) { homecoming getdata(context.ftcontractors.asqueryable()); } } public list<string> getparttime() { using (var context = new testdbentities()) { homecoming getdata(context.ptcontractors.asqueryable()); } } }

entities:

public interface icontractor { int id { get; set; } string name { get; set; } } public partial class ftcontractor : icontractor { public int id { get; set; } public string name { get; set; } } public partial class ptcontractor : icontractor { public int id { get; set; } public string name { get; set; } }

test:

[testmethod] public void temp() { var tester = new repository(); var ft = tester.getfulltime(); var pt = tester.getparttime(); assert.areequal(3, ft.count); assert.areequal(4, pt.count); }

in database there 2 tables containing id , name columns

c# linq entity-framework

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -