C# BindingList 和 LINQ?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/318644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
BindingList and LINQ?
提问by Patrick Desjardins
I am new with Linq and I would like to sort some data that are in the BindingList. Once I did my Linq query, I need to use back the BindingList collection to bind my data.
我是 Linq 的新手,我想对 BindingList 中的一些数据进行排序。完成 Linq 查询后,我需要使用回 BindingList 集合来绑定我的数据。
var orderedList = //Here is linq query
return (BindingList<MyObject>)orderedList;
This compiled but fails in execution, what is the trick?
这编译但执行失败,有什么技巧?
采纳答案by leppie
new BindingList<MyObject>(orderedList.ToList())
回答by leppie
That above only works when your linq query's select projection is explicitly typed as MyObject rather than select new which creates an instance of an anonymous object. In such cases the typeof(orderedList.ToList()) winds up as something akin to this: System.Collections.Generic.List<<>f__AnonymousType1>
仅当您的 linq 查询的 select 投影显式键入为 MyObject 而不是 select new 时才有效,这会创建匿名对象的实例。在这种情况下, typeof(orderedList.ToList()) 类似于以下内容: System.Collections.Generic.List<<>f__AnonymousType1>
ie: this should work:
即:这应该有效:
var result = (from x in MyObjects
where (wherePredicate( x ))
select new MyObject {
Prop1 = x.Prop1,
Prop2 = x.Prop2
}).ToList();
return new BindingList<MyObject>( result );
this will not:
这不会:
var result = from x in db.MyObjects
where(Predicate(x))
select new {
Prop1 = x.Prop1
Prop2 = x.Prop2
};
return new BindingList<MyObject>(result.ToList())
//creates the error: CS0030 "Cannot convert type 'AnonymousType#1' to 'MyObject'
In the second case they typeof(result) is: System.Collections.Generic.List<<>f__AnonymousType2> (the type params match the properties set in your select projection)
在第二种情况下,它们的 typeof(result) 是: System.Collections.Generic.List<<>f__AnonymousType2> (类型参数与您选择投影中设置的属性相匹配)
参考:http: //blogs.msdn.com/swiss_dpe_team/archive/2008/01/25/using-your-own-defined-type-in-a-linq-query-expression.aspx
回答by KyleMit
You can't always cast any collection type into any other collection. In terms of when the the compiler checks casting, check out this post on Compile-time vs runtime casting
您不能总是将任何集合类型转换为任何其他集合。关于编译器何时检查强制转换,请查看关于编译时与运行时强制转换的这篇文章
However, you can easily produce a BindingList
from an enumerable by doing some of the plumbing yourself. Just add the following Extension Method onto any Enumerable type to convert the collection into a BindingList.
但是,您可以BindingList
通过自己进行一些管道操作,轻松地从可枚举中生成 a 。只需将以下扩展方法添加到任何 Enumerable 类型即可将集合转换为 BindingList。
C#:
C#:
static class ExtensionMethods
{
public static BindingList<T> ToBindingList<T>(this IEnumerable<T> range)
{
return new BindingList<T>(range.ToList());
}
}
//use like this:
var newBindingList = (from i in new[]{1,2,3,4} select i).ToBindingList();
VB:
VB:
Module ExtensionMethods
<Extension()> _
Public Function ToBindingList(Of T)(ByVal range As IEnumerable(Of T)) As BindingList(Of T)
Return New BindingList(Of T)(range.ToList())
End Function
End Module
'use like this:
Dim newBindingList = (From i In {1, 2, 3, 4}).ToBindingList()