0

I am using the following query in vb.net using the System.Linq.Dynamic library to bind the result to a gridview

Dim customers = model.Customers _
    .OrderBy(sortExp + " " + sortOrder) _
    .Select("new (CustomerID, CompanyName, ContactName, ContactTitle, Address, City,     Region, PostalCode, Country, Phone, Fax)")

grdCustomers.DataSource = customers
grdCustomers.DataBind()

The following error is displayed

Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery) 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().

The result returned is of type DbQuery but I cannot do a ToList() on the result.

There must be a cleaner way to create a list instead of interating through the customers and creating the list manually.

Kuv Patel
  • 20
  • 5
  • what you use? webforms, winforms,wpf or something else? – Grundy Apr 15 '14 at 14:01
  • I am using webforms, vs2012 – Kuv Patel Apr 16 '14 at 14:12
  • you can try `grdCustomers.DataSource = list.Cast().AsEnumerable();` also it seems on this question [binding-gridview-to-iqueryablet](http://stackoverflow.com/questions/5197106/binding-gridview-to-iqueryablet), because DymanicLinq query return `IQueryable` – Grundy Apr 16 '14 at 14:29
  • I tried grdCustomers.DataSource = customers.Cast(Of DynamicClass).AsQueryable() and grdCustomers.DataSource = customers.Cast(Of Employee).AsQueryable() but still get the same error. Also note I have changed a mistake in the question. Datasource is assigned customers variable and not the list variable. When I check the value of customers in watch window the result is of type System.Data.Entity.Infrastructure.DbQuery(Of System.Linq.Dynamic.DynamicQueryable). Basically I want a typed list of employees. – Kuv Patel Apr 18 '14 at 08:03
  • in my sample `AsEnumerable()` why you try `AsQueryable()`? :-) – Grundy Apr 18 '14 at 08:31
  • try `grdCustomers.DataSource = list.Cast(Of dynamic)().AsEnumerable();` – Grundy Apr 18 '14 at 08:33
  • I tried grdCustomers.DataSource = list.Cast(Of DynamicClass)().AsEnumerable() and got the same error. in vb.net I am not getting (Of dynamic) in intellisense therefore I am using DynamicClass. – Kuv Patel Apr 21 '14 at 20:17
  • try `Cast(Of Object)` – Grundy Apr 22 '14 at 07:45
  • also tried this but I get the same error. further reading on the web suggests that it is not possible to convert the result of a query from dynamic linq to a List type when working with Linq to Entities. It is only possible to do this if the data is coming from Linq to Sql. There is LINQKit which will do the job of system.linq.dynamic library. – Kuv Patel Apr 26 '14 at 09:18

0 Answers0