I have the model below and Would like to order my list of Categories by Product.CreationDate Desc ?
var result = this.ListCategory.OrderBy(c => c.LisProduct.OrderByDescending(p => p.CreationDate));
does not work
public class Category
{
public int id { get; set; }
public string name { get; set; }
public virtual List<Product> LisProduct { get; set; }
}
public class Product
{
public int id { get; set; }
public string name { get; set; }
public DateTime CreationDate { get; set; }
public virtual Category Category { get; set; }
}
Goraleye
Member
295 Points
370 Posts
Order by navigation properties
Mar 02, 2012 08:54 AM|LINK
Hi,
I have the model below and Would like to order my list of Categories by Product.CreationDate Desc ?
public class Category { public int id { get; set; } public string name { get; set; } public virtual List<Product> LisProduct { get; set; } } public class Product { public int id { get; set; } public string name { get; set; } public DateTime CreationDate { get; set; } public virtual Category Category { get; set; } }Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Order by navigation properties
Mar 04, 2012 12:43 AM|LINK
Hello :)
Since your Product has a Category (mapping to the Category)——So you can have a try like this:
var result = (from p in XXX.Products
order p by p.CreationDate
select p.Category.name).Distinct();
Goraleye
Member
295 Points
370 Posts
Re: Order by navigation properties
Mar 04, 2012 09:44 AM|LINK
Ok,
I will try this if work
But I will select select p.Category and not select p.Category.name
Because I need other properties of Category.
Best regards
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Order by navigation properties
Mar 05, 2012 12:22 AM|LINK
Hello again:)
If you can get the instance of Category from Product,You should get other public properties from Category instance。
Goraleye
Member
295 Points
370 Posts
Re: Order by navigation properties
Mar 12, 2012 08:35 AM|LINK
Hi this does not work
Because I've list of categories and I would like to order by product.dtcreation and return again my list of categories.
For sample : Category with product creation date at 02/01/2012 must be displayed after Category with product creation date at 01/01/2012
var result = (from c in listCategories orderby c.Products.OrderByDescending(p => p.DtCreation) select c);TimoYang
Contributor
3732 Points
1275 Posts
Re: Order by navigation properties
Mar 12, 2012 08:47 AM|LINK
You mean you cannot get c as Category?
Goraleye
Member
295 Points
370 Posts
Re: Order by navigation properties
Mar 12, 2012 09:10 AM|LINK
No,
I can get C as Category but the oder by does not return a good shorted list categories
TimoYang
Contributor
3732 Points
1275 Posts
Re: Order by navigation properties
Mar 12, 2012 09:14 AM|LINK
What do u want?You want to sort Category by Products?