Answer the question
In order to leave comments, you need to log in
How to make a selection from the database using the Entity Framework?
Hello, I am using Entity Framework. In my wfa application, there is a DataGridView, to which I set the following DataSource:
productBindingSource.DataSource = context.Products.ToList();
metroGrid1.DataSource = productBindingSource;
var products = context.Products.Select(prd=> new
{
prd.id,
prd.name,
prd.price,
Menu = prd.Menu.name
});
productBindingSource.DataSource = products.ToList();
metroGrid1.DataSource = productBindingSource;
productBindingSource.Add(frm.ProductInfo);
public Product ProductInfo
{
get
{
return productBindingSource.Current as Product;
}
}
Answer the question
In order to leave comments, you need to log in
The problem is that the query returns objects of an anonymous class, and when adding and changing, the Product class is passed. To solve the problem, there are two options:
1. Convert the received data from the anonymous class to Product
AsEnumerable() (you can use ToList()) is called to execute the request, without it you will receive an Exception, because EF will not be able to parse such a Select().
2. Added and changed data should be converted from Product to an anonymous class
productBindingSource.Add(
new
{
frm.ProductInfo.id,
frm.ProductInfo.name,
frm.ProductInfo.price,
Menu = frm.ProductInfo.Menu.name
});;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question