is it possible?
Public String Get_Filed_By_Id(string table_Name,String Field_Name,string PK_val)
{
string strRes="";
using(mydbcontext db=new mydbcontext())
{
var x=db.table_Name.Where(p=>p.Id=PK_val).FirstOrDefault().Field_Name;
strRes=Convert.Tostring(x);
}
return strRes;
}
OR
var x=(from o in db.table_Name where o.Id=PK_val select o.Field_Name).FirstOrDefault();
Here, i'm passing Table_Name
,Column_Name
and the Condition value(PK_val
) to Get the Column_Name
from Table_Name
within a Certain Condition(Id=Pk_val
).
Is it possible??
Yes, it is.
First, some helpers:
and now your function:
I had this question too ,I know this is not exactly what you want and you need write more code but it's much cleaner than those you want to write.
Using repository pattern
For every table you should have a model class and Repository class.
Consider this code(this code from one of my project)
This is my comment table(this can be anything with or without navigation property)
RepositoryComment
and a base class that you send your query with table name(here model) and field(you can extend clas for more functionality)
For any other table just make model class and reposiotry then inherite from base class
Using code
No, but in this way
It is not really possible with EntityFramework actually(as far as I know). If you only needed the field by its name, then you could have used @Den's proposed solution. But you want to specify the table name too as a parameter. So I suggest you to use standard Sql Connector api, and build the query string with the parameters you provide.
Check this link for usage of standard sql connector api.