0

I currently have an array of "Brand Products" that I am trying to sort into alphabetical order based on their name after they are obtained from the database, the list is currently populated using this:

    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;
    }

5 Answers 5

2

You can achieve it in a single linq expression.

products[] productList = criteria.List<Product>()
                                  .Where(p => p.Parent != null)
                                  .OrderBy((p => p.Name)
                                  .ToArray();

Comple code.

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

I actually used an alternative method by doing most of it in NHibernate :) but thank for you the answer and I am sure this will help other people with similar issues!
0

I'd simply replace the 2nd half of your method by:

return filteredProductList.OrderBy(f => f.BrandName).ToArray();

assuming that BrandName is what you want to order by

Comments

0

Something roughly like this, depending on your types:

var products = from p in GetCollection() orderby p.ProductName select p;

Comments

0

If you keep it as a List, then you can use linq against the collection...

mylist = myList.OrderBy(x => x.Name).ToList();

Comments

0

Try this:

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();
}

1 Comment

How about filtering by Parent != null, as in the original?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.