[英]use LINQ for queries on BindingSource
I want to use Linq for queries on Binding Source. 我想在绑定源上使用Linq进行查询。
var myItems = ((IList<myObjectBinding>)myTableBindingSource.List).Where(some linq query);
This code gives the following error during runtime: Unable to cast object of type 'System.Data.DataView' to type 'System.Collections.Generic.IList 此代码在运行时给出以下错误:无法将类型为“ System.Data.DataView”的对象转换为类型为“ System.Collections.Generic.IList”
So, Is it possible to use Linq for queries on binding source? 因此,是否可以使用Linq进行绑定源查询? I want to use binding source objects elsewhere in the code.
我想在代码的其他地方使用绑定源对象。
first you have to get all records from bindingSource.DataSource in a list like 首先,您必须从bindingSource.DataSource的列表中获取所有记录,例如
var list = myBindingSource.List.OfType<yourmodel/class>();
than you can use query on this list like 比您可以在此列表上使用查询
var records = list.where(u => u.id == yourid).ToList();
您可以只编写一个转换器,然后在转换器中添加LINQ逻辑。
You need to cast to the generic version of IList using the Cast extension: 您需要使用Cast扩展名将其转换为IList的通用版本:
((IList)myTableBindingSource.List).Cast<myObjectBinding>().Where(some linq query);
DataView inherits from System.Collections.IList , but you are trying to cast to System.Collections.Generic.IList . DataView继承自System.Collections.IList ,但是您尝试转换为System.Collections.Generic.IList 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.