c# - Creating a method that queries a database using linq -


i have multiple methods pretty same thing query different tables.

i trying use 1 method , pass method parameters instead can skip creating multiple methods , neaten code.

this have currently:

myentitymodel entity = new myentitymodel();  private void loaddata() {     var query = data in entity.table1                select data;      mydatagrid.itemssource = query.tolist(); } 

i want this:

myentitymodel entity = new myentitymodel();  private void loaddata(datagrid datagrid, table mytable ) {     var query = data in entity.mytable                select data;      datagrid.itemssource = query.tolist(); } 

i'm unsure type parameter should pass mytable tried passing system.data.entity.dbset , myentitymodel

is possible using linq?

you can create generic method returns data dbset instance of provided entity type:

myentitymodel entity = new myentitymodel();  private void loaddata<t>(datagrid datagrid)     t : class {         datagrid.itemssource = entity.set<t>().tolist(); } 

use like

loaddata<table1>(mydatagrid); // table1 entity type name 

Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -