Простой вопрос относительно LINQ - PullRequest
0 голосов
/ 16 ноября 2010

У меня есть эта проблема в реализации 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 Ответ

16 голосов
/ 16 ноября 2010

Вы действительно читали эти сообщения об ошибках?

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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...