I have a database and I need to pull data from it to WinForm. I can pull data from one column but I need from multiple columns. How to do this on Entity Framework. This is my code:
private void button2_Click(object sender, EventArgs e)
{
using (var context = new spEntities())
{
var Da2 = context.Database.SqlQuery<string>("Select distinct kg from spower ").ToList<string>();
listBox3.DataSource = Da2;
}
}
And This is what I need:
private void button2_Click(object sender, EventArgs e)
{
using (var context = new spEntities())
{
var Da2 = context.Database.SqlQuery<string>("Select distinct kg,rep from spower ").ToList<string>();
listBox3.DataSource = Da2;
}
}
What would be easiest or the best way to get data from multiple columns.
You have to capture the results into a class with matching property names, and (at least) a parameterless constructor:
class DbResult
{
public int ID { get; set; }
public string NAME { get; set; }
public string DB_FIELD { get; set; }
}
var result = _dbContext.Database.SqlQuery<DbResult>(
"select ID, NAME, DB_FIELD from eis_hierarchy");