关于LINQ的简单问题

时间:2010-11-16 18:33:53

标签: c# linq linq-to-sql

我在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 个答案:

答案 0 :(得分: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.Coreusing添加对System.LinqSystem.Data.Linq指令的引用。