2010-11-16 95 views
0

我在LINQ实现中遇到了这个问题。我得到的错误代码中的倾斜的评论关于LINQ的简单问题

public partial class _Default : Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!this.IsPostBack) 
     { 
      GridViewBind(string.Empty); 
     } 
    } 

    private void GridViewBind(string criteria) 
    { 
     string strConn = ConfigurationManager.ConnectionStrings["linqconnstr"].ConnectionString; 

     MyDB _db = new MyDB(strConn); 

     IEnumerable<UserRecord> results; 

     if(criteria == string.Empty) 
     { 
      // 'System.Data.Linq.Table<UserRecord>' does not contain a definition  
      // for 'ToArray' and no extension method 'ToArray' accepting a first 
      // argument of type 'System.Data.Linq.Table<UserRecord>' could be found 
      // (are you missing a using directive or an assembly reference?) 
      results = _db.user.ToArray(); // error line under .ToArray(); 
     } 
     else 
     { 
      // Could not find an implementation of the query pattern for source 
      // type 'System.Data.Linq.Table<UserRecord>'. 'Where' not found. 
      // are you missing a reference to 'System.Core.dll' or a using 
      // directive for 'System.Linq'? // error line under _db 
      results = (from c in _db.user 
         where c.Username.Contains(criteria) 
         select c).ToArray(); 
     } 
     gvwUsers.DataSource = results; 
     gvwUsers.DataBind(); 
    } 
} 

下面是其他类:

public class MyDB : DataContext 
{ 
    public MyDB(string connStr) : base(connStr) 
    { 
    } 

    public Table<UserRecord> user; 
} 

[Table(Name = "tblUsers")] 
public class UserRecord 
{ 
    [Column(IsDbGenerated = true, IsPrimaryKey = true)] 
    public int UserID { get; set; } 

    [Column(DbType = "nvarchar(30)")] 
    public string Username { get; set; } 

    [Column(DbType = "nvarchar(30)")] 
    public string Password { get; set; } 
} 

回答

16

你真的读这些错误信息?

// (are you missing a using directive or an assembly reference?) 

// are you missing a reference to 'System.Core.dll' or a using 
// directive for 'System.Linq'? 

尝试增加至System.Core一个基准和一个using指令为System.Linq和可能System.Data.Linq

+0

该死的,打我18秒:D – 2010-11-16 18:37:14

+1

当然,我做到了。我添加了Data.Linq命名空间,但System.Linq是解决方案。谢谢。 +1 – 2010-11-16 18:48:58