这是我需要的代码,我需要在绑定之前将其排序为升序。这是从一个定义了汽车类型的不同控件调用的,并且附加到此公共控件中的Searcher属性。所以在这里我尝试将它转换为一个列表进行排序,计数变为0.所以我不确定如何在绑定到网格之前通过汽车类型的参数对其进行排序。
IEnumerable cars = this.Searcher.Search(_enteredText); //cars count 70
var carBL = Generics.CreateGeneric<IBindingList>(typeof(IMBindingList<>),
cars.GetType().GetGenericArguments()[0]);
grdResults.DataSource = carBL ;
我的代码试图对那些无效的结果进行排序。搜索后,汽车返回70。
var orderedcars = cars.ToEnumerable().ToList().OrderBy(x = ?) ;
carBL计数正在变为0.我在排序和下一行时没有做正确的事情。我想我需要使用ApplySort但不确定propertydescriptor应该是什么。
搜索代码(文字)
public virtual IEnumerable<T> Search(string filter)
{
List<T> somelist = new List<T>(); ;
if (lastsomelist != null && this.Searchsomelist)
{
somelist = this.SearchtheList(lastsomelist , filter);
}
else
{
somelist= this.SearchtheList(this.commoncontrol, filter);
if ((somelist.Count == 0 )
somelist.AddRange(this.SearchtheList(this.multiSearchtheList, filter));
}
if (somelist!= null && somelist.Count > 0)
lastsomelist = somelist;
else
lastsomelist = null;
return somelist;
}
这样做会对你有用吗?
IEnumerable cars = abc.Searcher.Search(_enteredText);
var orderedcars = cars.Cast<ModelClass>().ToList().OrderBy(x => x.Name);
abc.LoadallMacthingInventory(orderedcars.AsEnumerable());
首先,这里不需要orderedcars.AsEnumerable();
。 OrderBy(x => x.Name);
子句返回IOrderedEnumerable<TElement>
,它转而来自IEnumerable<TElement>
,所以你可以直接将orderedcars
传递给像LoadallMacthingInventory
这样的abc.LoadallMacthingInventory(orderedcars);
方法。因此,观察orderedcars.AsEnumerable()
没有产生结果不是由于AsEnumerable()
。相反,你应该看看cars.Cast<ModelClass>().ToList().OrderBy(x => x.Name).Count();
是否超过零。如果它是零,我怀疑是abc.Searcher.Search(_enteredText);
给你一个空列表。你可以通过检查abc.Searcher.Search(_enteredText).Count();
来确认。
我在使用ApplySort对列表进行排序,然后以这种方式绑定到网格。希望这有助于某人。
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(carBL[0]);
PropertyDescriptor myProperty = properties.Find("Name", false);
carBL.ApplySort(myProperty, ListSortDirection.Ascending);
grdResults.DataSource = carBl ; //all sorted!!