如何修复错误“CS1503 C#Argument 2:无法从'CoffeeShopApp.CoffeeShopDBEntities'转换为'System.Data.Entity.Core.Objects.ObjectContext'" [英] How can fix the error "CS1503 C# Argument 2: cannot convert from 'CoffeeShopApp.CoffeeShopDBEntities' to 'System.Data.Entity.Core.Objects.ObjectContext'"
本文介绍了如何修复错误“CS1503 C#Argument 2:无法从'CoffeeShopApp.CoffeeShopDBEntities'转换为'System.Data.Entity.Core.Objects.ObjectContext'"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Entity.Core.Objects;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CoffeeShopApp
{
public partial class frmViewProduct : Form
{
CoffeeShopDBEntities cse = new CoffeeShopDBEntities();
public frmViewProduct()
{
InitializeComponent();
dgv.DataSource = cse.tblProducts.ToList();
dgv.Columns["ProductType"].Visible = false;
dgv.Columns["tbleProductType"].Visible = false;
dgv.Columns["tblTransactionItems"].Visible = false;
}
private void cboProductType_SelectionChangeCommitted(object sender, EventArgs e)
{
ObjectQuery<tblProduct> filteredProducts = new ObjectQuery<tblProduct>(
"select value product from tblProduct as product where product.ProductType =" + cboProductType.SelectedValue, cse);--->> I got error here with cse
dgv.DataSource = filteredProducts;
}
}
}
推荐答案
尝试以下流程:
通过实体实体SQL :
Try with below processes:
Through entity Entity SQL:
//Querying with Object Services and Entity SQL
string sqlString = "SELECT VALUE st FROM CoffeeShopDBEntities.tblProduct AS st WHERE st.ProductType == '" + cboProductType.SelectedValue+ "'";
var objctx = (ctx as IObjectContextAdapter).ObjectContext;
ObjectQuery<tblproduct> filteredProducts = objctx.CreateQuery<tblproduct>(sqlString);
</tblproduct></tblproduct>
通过 Native SQL :
Through Native SQL:
using (var ctx = new CoffeeShopDBEntities())
{
var filteredProducts = ctx.tblProduct.SqlQuery("select value product from tblProduct as product where product.ProductType = '" + cboProductType.SelectedValue + "'").ToList<tblproduct>();
}
</tblproduct>
注意:上面的代码编写为假设实体名称为tblProduct。如果它与EF中的Products不同,则更改它。
Note: Above code written as assuming the entity name as tblProduct. If it is different like "Products" in EF then change it.
请参阅此处的示例 - 在ADO.NET中使用DataGridView进行数据绑定 [ ^ ]。
See the example here - Databinding with DataGridView in ADO.NET[^].
这篇关于如何修复错误“CS1503 C#Argument 2:无法从'CoffeeShopApp.CoffeeShopDBEntities'转换为'System.Data.Entity.Core.Objects.ObjectContext'"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文