代码之家  ›  专栏  ›  技术社区  ›  Ken D

关于LINQ的简单问题

  •  0
  • Ken D  · 技术社区  · 15 年前

    我在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; }
    }
    
    1 回复  |  直到 15 年前
        1
  •  16
  •   jason    15 年前

    你真的读过这些错误信息吗?

    // (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 .