[英]Return Linq result to a dataset - GetCommand Is Not a Property
I'm trying the first solution mentioned in - http://forums.asp.net/t/1320587.aspx , to return the linq result to a dataset. 我想提到的第一个解决方案- http://forums.asp.net/t/1320587.aspx ,对LINQ结果返回到数据集。 But I dont get the 'GetCommand' property after 'mdb'. 但是我没有在'mdb'之后得到'GetCommand'属性。 The error says MedianEntities does not contain a definition for 'GetCommand'. 该错误表明MedianEntities不包含“ GetCommand”的定义。 Are you missing an assembly. 您是否缺少大会。
What else should I include to fix this. 我还应该包括什么来解决此问题。
public DataSet GetAllRecords()
{
DataSet ds = new DataSet();
MEDIANEntities mdb = new MEDIANEntities();
var query = (from j in mdb.tblCountries
orderby j.CountryName ascending select j);
SqlCommand cmd = (SqlCommand)mdb.GetCommand(query); //error here
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
return ds;
}
Using .Netframework 4.0 and Entity Model 使用.Netframework 4.0和实体模型
DataContext and GetCommand is for LINQ-To-SQL, you want to work with LINQ-To-Entity. DataContext和GetCommand用于LINQ-To-SQL,您想使用LINQ-To-Entity。
There are multiple issues, your entities MEDIANEntities could be DbContext, the GetCommand is on DataContext class. 存在多个问题,您的实体MEDIANEntities可能是DbContext,GetCommand在DataContext类上。
Another issue is that your "query" is not an IQueryable, need to remove your .AsEnumerable() from your query. 另一个问题是您的“查询”不是IQueryable,需要从查询中删除.AsEnumerable()。
You can use CopyToDataTable<DataRow>()
on your query
instead to accomplish what you need. 您可以在query
上使用CopyToDataTable<DataRow>()
来完成所需的操作。 http://msdn.microsoft.com/en-us/library/bb396189.aspx http://msdn.microsoft.com/en-us/library/bb396189.aspx
Another option, which should work but might be hacky, as you are mixing DataContext (linq-to-sql) with (linq-to-entity). 当您将DataContext(linq-to-sql)与(linq-to-entity)混合使用时,另一种方法应该可行,但可能会很麻烦。 Something like that: 像这样:
using (DataContext ctx = new DataContext(mdb.Database.Connection))
{
DataSet ds = new DataSet();
var query = (from j in mdb.tblCountries
orderby j.CountryName ascending
select j);
SqlCommand cmd = (SqlCommand)ctx.GetCommand(query);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
return ds;
}
More info on GetCommand: 有关GetCommand的更多信息:
DataContext.GetCommand DataContext.GetCommand
Namespace: System.Data.Linq 命名空间:System.Data.Linq
Assembly: System.Data.Linq (in System.Data.Linq.dll) 程序集:System.Data.Linq(在System.Data.Linq.dll中)
http://msdn.microsoft.com/en-us/library/system.data.linq.datacontext.getcommand.aspx http://msdn.microsoft.com/zh-CN/library/system.data.linq.datacontext.getcommand.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.