最近在練習EF的過程中,實現將Linq返回的結果綁定到Combox時出錯。 錯誤提示如下: System.NotSupportedException:“Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery, DbRawS... ...
最近在練習EF的過程中,實現將Linq返回的結果綁定到Combox時出錯。
錯誤提示如下:
System.NotSupportedException:“Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery, DbRawSqlQuery) is not supported. Instead populate a DbSet with data, for example by calling Load on the DbSet, and then bind to local data. For WPF bind to DbSet.Local. For WinForms bind to DbSet.Local.ToBindingList(). For ASP.NET WebForms you can bind to the result of calling ToList() on the query or use Model Binding, for more information see http://go.microsoft.com/fwlink/?LinkId=389592.”
折騰了許久,把查詢的結果轉換等,最終發現ComBoBox可以綁定Linq查詢的結果,唯一的要求就將DataSource放在DisplayMember和ValueMember的後面。
private void SearchBtn_Click(object sender, EventArgs e) { //讀取表數據並綁定Combox空間 using (OrderDBContainer db = new OrderDBContainer()) { CBox_User.DisplayMember = "Name"; CBox_User.ValueMember = "Id"; CBox_User.DataSource = from u in db.UserAccoutSet orderby u.LastName select new { Name = u.LastName + ", " + u.FirstName, Id = u.Id }; } }
打賞支付寶: