扩展 IEnumerable 以返回 BindingList
在上一个关于 Stack Overflow 的问题中,我遇到了返回问题对 DataGridView 的 EF 查询。当然,我会遇到一个问题。然而,我添加了一个扩展方法,但它仍然让我感到困惑,因为它不起作用。看起来应该是这样,但由于某种原因并非如此。
public static class BindingListEntityExtension
{
public static BindingList<T> ToBindingList<T>(this IEnumerable<T> entities)
{
BindingList<T> rtn = new BindingList<T>();
foreach (T obj in entities)
{
rtn.Add(obj);
}
return rtn;
}
}
有什么想法吗?我的实现是这样的:
MyEntities context = new MyEntities();
tempDataGridView.DataSource = context.Employees.ToBindingList();
In a previous question on Stack Overflow, I had run into an issue with returning an EF query to the DataGridView. Of course I'd run into an issue. However, I added an extension method that still has me baffled since it isn't working. It seems like it should, but for some reason it's not.
public static class BindingListEntityExtension
{
public static BindingList<T> ToBindingList<T>(this IEnumerable<T> entities)
{
BindingList<T> rtn = new BindingList<T>();
foreach (T obj in entities)
{
rtn.Add(obj);
}
return rtn;
}
}
Any ideas what's going on? My implementation is like so:
MyEntities context = new MyEntities();
tempDataGridView.DataSource = context.Employees.ToBindingList();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
知道了。正如 Ecyrb 在上一篇文章中所建议的,BindingList 不会排序。我确实使用了建议的网站/来对我的列表进行排序。谢谢你们!我的扩展现在可以工作了。
Got it. As Ecyrb had suggested in a previous post, the BindingList does not sort. I did use the suggested site/ to get my list to sort. Thanks guys! My extension does work now.