2010-11-16 137 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