none
Not being able to capture anonymous type result return from SqlQuery execution EF code first RRS feed

  • Question

  • i try to execute this code. i got no error but could not get desired result. see my code

    private async void button1_Click(object sender, EventArgs e) { using (var db = new TestDBContext1()) { var customers = await db.Database.SqlQuery<dynamic>("AllCustomers").ToListAsync();

    dataGridView1.DataSource = customers; } }

    when i try to debug the customer then found no customer data in it but right result count was showing. when bind customer to datagridview then no result shown.

    just do not understand why dynamic is not working here ?

    i need something which store customer data to customers variable and i should query customers variable and should be able to bind customers variable to grid.help me with knowledge.

    thanks

    Monday, September 26, 2016 9:21 AM

All replies

  • Hi Mou_inn,

    According to your description, I create a demo and reproduce your issue on my side, it seems that variable named customers is a object collection, which could not cast to customer object.

    I would suggest you could use special model instead of dynamic type. like this:

    using (var db = new EFDemoContext())
    {
       var customers = await db.Database.SqlQuery<Customer>("AllCustomers").ToListAsync();          
       dataGridView1.DataSource = customers;
    }

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Tuesday, September 27, 2016 1:56 AM
    Moderator