C# – Query that returns Entity and child collection.. child collection sorted

clinq-to-entities

How can i create a query that will return a single entity along with a collection, and have the collection sorted? i don't care if it's two calls to the DB.. i just need the end result to be an Entity type and not an anonymous type:

var

category = context.Categories.Where(c => c.categoryID == categoryID).Select(c => new { c, products = c.Products.OrderByDescending(p => p.priceDate) }).First();

But i'd like to have the above either return or cast to a Category with the Products collections.. as apposed to anonymous type. thanks!

Best Answer

Could you just put that into the category class? Something like this:

public parital class Category 
{
        public Ilist<Product> ProductsByPriceDate
        {
          get
          {
              return this.Products.OrderbyDescending(p= > p.priceDate).ToList();
          }
        }
    }
Related Topic