我正在尝试从数据库中检索整个表到我的数据网格视图中。我找到了this article,但在我的Windows窗体项目中并不存在var q = from a in emp.GetTable<Employee>() select a;
这里我有一个错误
错误1&#39; EmployeeList.EmployeeEntities1&#39;不包含&#39; GetTable&#39;的定义没有扩展方法&#39; GetTable&#39;接受类型&#39; EmployeeList.EmployeeEntities1&#39;的第一个参数。可以找到(你错过了使用指令或程序集引用吗?)
我该如何解决?
我已添加using System.Data.Linq;
,但一切都没有改变..
有没有其他方法可以将整个表格检索到我的数据网格视图中?或者我该如何改进呢?
答案 0 :(得分:1)
只是查看错误emp应该从类定义中的DataContext继承。 DataContext具有GetTable方法。
这是一个可以帮助你的链接。
http://msdn.microsoft.com/en-us/library/bb399375(v=vs.110).aspx
// DataContext takes a connection string.
DataContext db = new DataContext(@"c:\Northwnd.mdf");
// Get a typed table to run queries.
Table<Customer> Customers = db.GetTable<Customer>();
// Query for customers from London.
var query =
from cust in Customers
where cust.City == "London"
select cust;
foreach (var cust in query)
Console.WriteLine("id = {0}, City = {1}", cust.CustomerID, cust.City);
使用继承自DataContext
的类的模型public partial class Northwind : DataContext
{
public Table<Customer> Customers;
public Table<Order> Orders;
public Northwind(string connection) : base(connection) { }
}
然后在您的控制器中。
Northwnd db = new Northwnd(@"c:\Northwnd.mdf");
var query =
from cust in db.Customers
where cust.City == "London"
select cust;
foreach (var cust in query)
Console.WriteLine("id = {0}, City = {1}", cust.CustomerID,
cust.City);