I have an IQueryable<Product>
that needs to be sorted by Name
. Each Product
has an IQueryable<Category>
that also needs to be sorted by Name
. I'm having a hard time expressing this in Linq. I could loop through the products and sort each category list, but it seems messy. Hoping a Linq ninja has a smarter solution.
My Product
class looks like:
public class Product {
public string Name { get; set; }
public IQueryable<Category> Categories { get; set; }
}
My Category
class looks like:
public class Category {
public string Name { get; set; }
}
Currently, I'm doing this:
var myProducts = products.OrderBy(x => x.Name).Select(x => new Product {
Name = x.Name,
Categories = x.Categories(y => y.Name)
});
The trouble is, when using something like NHibernate, this creates new Product objects, essentially disconnecting the product from the NH session.