C# 在实体框架中编写将数据返回到datagridView的函数
我是实体框架的新手。我的问题是我想编写一个函数,将结果查询返回到DataGridViewC# 在实体框架中编写将数据返回到datagridView的函数,c#,entity-framework,linq,C#,Entity Framework,Linq,我是实体框架的新手。我的问题是我想编写一个函数,将结果查询返回到DataGridView public IEnumerable<tblCustomerType> GetCustomerType() { // IEnumerable<tblCustomerType> result; using (var context = new dbLMSEntities()) { var x = (fro
public IEnumerable<tblCustomerType> GetCustomerType()
{
// IEnumerable<tblCustomerType> result;
using (var context = new dbLMSEntities())
{
var x = (from c in context.tblCustomerTypes
select new {c.CustomerTypeID, c.CustomerType}).AsEnumerable();
return x;
}
public IEnumerable GetCustomerType()
{
//可数结果;
使用(var context=new dblmsenties())
{
var x=(来自context.tblCustomerTypes中的c)
选择新的{c.CustomerTypeID,c.CustomerType});
返回x;
}
但我犯了错误
无法将类型'System.Collections.Generic.IEnumerable'
隐式转换为'System.Collections.Generic.IEnumerable'
您的方法返回特定类型:
public IEnumerable<tblCustomerType> GetCustomerType()
基于命名,听起来您的方法只是想直接返回记录。因此无需投影到其他类型:
var x = (from c in context.tblCustomerTypes
select c).AsEnumerable();
return x;
这实际上可以简化为:
return context.tblCustomerTypes;
理想情况下,您的方法将返回一个
IQueryable
,而它本身并没有实际实现任何记录。然后,如果您有其他下游逻辑,只需要数据的一个子集,则您将在那里执行该投影。将数据的物化推迟到您实际需要它时。您的方法将返回一个特定的数据fic类型:
public IEnumerable<tblCustomerType> GetCustomerType()
public IEnumerable<tblCustomerType> GetCustomerType()
{
// IEnumerable<tblCustomerType> result;
using (var context = new dbLMSEntities())
{
return context.tblCustomerTypes.ToList();
}
}
基于命名,听起来您的方法只是想直接返回记录。因此无需投影到其他类型:
var x = (from c in context.tblCustomerTypes
select c).AsEnumerable();
return x;
这实际上可以简化为:
return context.tblCustomerTypes;
理想情况下,您的方法将返回一个IQueryable
,而它本身并不实际实现任何记录。然后,如果您有其他下游逻辑,只需要数据的一个子集,则您将在那里执行该投影。推迟数据的物化,直到您实际需要它。public IEnumerable GetCustomerType()
public IEnumerable<tblCustomerType> GetCustomerType()
{
// IEnumerable<tblCustomerType> result;
using (var context = new dbLMSEntities())
{
return context.tblCustomerTypes.ToList();
}
}
{
//可数结果;
使用(var context=new dblmsenties())
{
返回context.tblCustomerTypes.ToList();
}
}
公共IEnumerable GetCustomerType()
{
//可数结果;
使用(var context=new dblmsenties())
{
返回context.tblCustomerTypes.ToList();
}
}
将函数返回类型更改为IQueryable
public IQueryable GetCustomerType()
{
// IEnumerable<tblCustomerType> result;
using (var context = new dbLMSEntities())
{
var x = (from c in context.tblCustomerTypes
select new
{
c.CustomerTypeID,
c.CustomerType
});
return x;
}
}
public IQueryable GetCustomerType()
{
//可数结果;
使用(var context=new dblmsenties())
{
var x=(来自context.tblCustomerTypes中的c)
选择新的
{
c、 CustomerTypeID,
c、 客户类型
});
返回x;
}
}
将函数返回类型更改为IQueryable
public IQueryable GetCustomerType()
{
// IEnumerable<tblCustomerType> result;
using (var context = new dbLMSEntities())
{
var x = (from c in context.tblCustomerTypes
select new
{
c.CustomerTypeID,
c.CustomerType
});
return x;
}
}
public IQueryable GetCustomerType()
{
//可数结果;
使用(var context=new dblmsenties())
{
var x=(来自context.tblCustomerTypes中的c)
选择新的
{
c、 CustomerTypeID,
c、 客户类型
});
返回x;
}
}
函数返回类型与您尝试返回的不同。您已将函数返回类型声明为返回IEnumerable,而实际返回的是匿名类的IEnumerable。函数返回类型与您尝试返回的不同。您已将函数返回类型声明为返回IEnumerable,而实际上是返回匿名类的IEnumerable。为什么要执行Select
而不是简单地返回context.tblCustomerTypes.ToList()
?我想选择一个特定的列only@Ajaxta然后更改您的函数返回类型。您需要将x插入到tblCustomer的新列表中并返回它。您没有这样说,并且在您的问题中找不到DataGridView。为什么要执行选择而不是简单地返回上下文。tblCustomerTypes.ToList()
?我想选择一个特定的列only@Ajaxta然后更改您的函数返回类型。您需要将x插入tblCustomer的新列表中并返回它。您没有这样说,并且在您的问题中找不到DataGridView。谢谢@David。谢谢您的解释谢谢@David。谢谢您的解释很好,好。标记为answer如果这能帮你解决问题。太好了,好的。如果这能帮你解决问题,请标记为答案。