Reputation: 999
I have a background worker that performs loading of data from the database into a temporary structure.
Data d = new Data();
d.listGroup = context.Groups.GetNewBindingList();
d.tbUser = context.Users.OrderBy(x => x.Name);
d.listPriceLevel = context.PriceLevels.GetNewBindingList();
e.Result = d;
The problem is that the 3rd line (d.tbUser = ... ) is being lazy-loaded. Sure, I can do:
context.Users.OrderBy( x => x.Name ).ToList();
But then again, this is not a Bindable List, any changes made to it won't propagate back to the DB.
So I think I need something like:
d.tbUser = context.Users.OrderBy( x => x.Name ).GetNewBindingList();
But that doesn't work. The goal is: retrieve a list of users, ordered by their name as a bind-able list. Any ideas?
Thanks for your time!
Upvotes: 2
Views: 752
Reputation: 185683
Adding OrderBy
(like any of the other query functions) turns your query into an IQueryable<TEntity>
. Fortunately, LINQ-to-SQL's internal query type (DataQuery<TEntity>
) provides a BindingList<TEntity>
via its implementation of IListSource
.
To get a BindingList
for a given query, you can do this:
var bindingList = ((IListSource)query).GetList();
In your case:
d.tbUser = ((IListSource)context.Users.OrderBy(x => x.Name)).GetList();
While the return type of GetList
is IList
, it is, in fact, an actual BindingList<User>
.
Upvotes: 3