Raw SQL query and return the results from database multiple columns c# Windows Forms

c# entity-framework-6 sqlite winforms

Question

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.

1
0
4/25/2017 6:52:39 AM

Accepted Answer

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");
46
1/20/2015 1:50:06 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow