问题描述
我尝试执行此代码。我没有错误,但无法获得理想的结果。看我的代码
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;
}
}
dataGridView1.DataSource = customers; }}
当我尝试调试客户时,发现其中没有客户数据但是结果数正确正在展示。将客户绑定到datagridview时,没有显示结果。
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 ?
我需要哪些商店客户数据到客户变量,我应该查询客户变量,并且应该能够将客户变量绑定到grid.help我的知识。
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
推荐答案
根据你的描述,我创建了一个演示并在我身边重现你的问题,似乎名为customers的变量是一个对象集合,它无法转换为客户对象。
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;
}
祝你好运,
Cole Wu
这篇关于无法首先从SqlQuery执行EF代码中捕获匿名类型结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!