我目前有一个"Brand Products"
数组,我试图根据他们从数据库中获取name
后按字母顺序排序,目前列表中填充了这个列表:
protected override object[] GetCollection()
{
Brand brand = ItemLocator.LocateItem(this.Parent, typeof(Brand)) as Brand;
ICriteria criteria = CoreHttpModule.Session.CreateCriteria(typeof(Product));
criteria.Add(NHibernate.Expression.Expression.Eq("Brand", brand));
criteria.Add(NHibernate.Expression.Expression.Eq("IsVisibleOnWebsite", true));
IList<Product> productList = criteria.List<Product>();
IList<Product> filteredLroductList = new List<Product>();
for (int i = 0; i < productList.Count; i++)
{
if (productList[i].Parent != null)
{
filteredLroductList.Add(productList[i]);
}
}
object[] filteredProductListArray = new object[filteredLroductList.Count];
for (int i = 0; i < filteredLroductList.Count; i++)
{
filteredProductListArray.SetValue(filteredLroductList[i], i);
}
return filteredProductListArray;
}
答案 0 :(得分:2)
您可以在单个linq表达式中实现它。
products[] productList = criteria.List<Product>()
.Where(p => p.Parent != null)
.OrderBy((p => p.Name)
.ToArray();
完成代码。
protected override object[] GetCollection()
{
Brand brand = ItemLocator.LocateItem(this.Parent, typeof(Brand)) as Brand;
ICriteria criteria = CoreHttpModule.Session.CreateCriteria(typeof(Product));
criteria.Add(NHibernate.Expression.Expression.Eq("Brand", brand));
criteria.Add(NHibernate.Expression.Expression.Eq("IsVisibleOnWebsite", true));
products[] productList = criteria.List<Product>()
.Where(p => p.Parent != null)
.OrderBy((p => p.Name)
.ToArray();
return productList;
}
答案 1 :(得分:0)
我只需用你的方法替换2 nd 的一半:
return filteredProductList.OrderBy(f => f.BrandName).ToArray();
假设BrandName
是您想要通过
答案 2 :(得分:0)
试试这个:
protected override object[] GetCollection()
{
Brand brand = ItemLocator.LocateItem(this.Parent, typeof(Brand)) as Brand;
ICriteria criteria = CoreHttpModule.Session.CreateCriteria(typeof(Product));
criteria.Add(NHibernate.Expression.Expression.Eq("Brand", brand));
criteria.Add(NHibernate.Expression.Expression.Eq("IsVisibleOnWebsite", true));
return criteria.List<Product>()
.Where(x => x.Parent != null)
.OrderBy(x => x.Name)
.Cast<object>()
.ToArray();
}
答案 3 :(得分:0)
大致相同的东西,取决于你的类型:
var products = from p in GetCollection() orderby p.ProductName select p;
答案 4 :(得分:0)
如果你把它保存为List,那么你可以使用linq对付集合......
mylist = myList.OrderBy(x =&gt; x.Name)。ToList();