OrderBy、GetNewBindingList 和 Linq to SQL
我有一个后台工作人员,负责将数据从数据库加载到临时结构中。
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;
问题是第三行 (d.tbUser = ... ) 正在被延迟加载。 当然,我可以这样做:
context.Users.OrderBy( x => x.Name ).ToList();
但话又说回来,这不是一个可绑定列表,对其所做的任何更改都不会传播回数据库。
所以我想我需要这样的东西:
d.tbUser = context.Users.OrderBy( x => x.Name ).GetNewBindingList();
但这不起作用。 目标是:检索用户列表,按其姓名排序作为可绑定列表。 有什么想法吗?
感谢您抽出时间!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
添加
OrderBy
(与任何其他查询函数一样)会将您的查询转换为IQueryable
。幸运的是,LINQ-to-SQL 的内部查询类型 (DataQuery
) 通过其IListSource
实现提供了BindingList
。要获取给定查询的
BindingList
,您可以执行以下操作:在您的情况下:
虽然
GetList
的返回类型是IList
,但它是事实上,一个实际的BindingList
。Adding
OrderBy
(like any of the other query functions) turns your query into anIQueryable<TEntity>
. Fortunately, LINQ-to-SQL's internal query type (DataQuery<TEntity>
) provides aBindingList<TEntity>
via its implementation ofIListSource
.To get a
BindingList
for a given query, you can do this:In your case:
While the return type of
GetList
isIList
, it is, in fact, an actualBindingList<User>
.