Order by navigation properties http://forums.asp.net/t/1775913.aspx/1?Order+by+navigation+properties+Mon, 12 Mar 2012 09:14:20 -040017759134860377http://forums.asp.net/p/1775913/4860377.aspx/1?Order+by+navigation+properties+Order by navigation properties <p>Hi,</p> <p>I have the model below and Would like to&nbsp; order my list of Categories by&nbsp; Product.CreationDate Desc ?</p> <pre class="prettyprint">var result = this.ListCategory.OrderBy(c =&gt; c.LisProduct.OrderByDescending(p =&gt; p.CreationDate));</pre> <pre class="prettyprint">does not work</pre> <pre class="prettyprint"> public class Category { public int id { get; set; } public string name { get; set; } public virtual List&lt;Product&gt; 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; } }</pre> 2012-03-02T08:54:33-05:004862418http://forums.asp.net/p/1775913/4862418.aspx/1?Re+Order+by+navigation+properties+Re: Order by navigation properties <p>Hello </p> <p>Since your Product has a Category mapping to the CategorySo you can have a try like this</p> <p>var result = (from p in XXX.Products</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;order p by p.CreationDate</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;select p.Category.name).Distinct();</p> 2012-03-04T00:43:16-05:004862702http://forums.asp.net/p/1775913/4862702.aspx/1?Re+Order+by+navigation+properties+Re: Order by navigation properties <p>Ok,&nbsp;</p> <p>I will try this if work</p> <p>But I will select&nbsp;<span>select p.Category &nbsp;and not &nbsp;<span>select p.Category.name</span></span></p> <p><span><span>Because I need other properties of Category.</span></span></p> <p><span><span>Best regards</span></span></p> 2012-03-04T09:44:55-05:004863162http://forums.asp.net/p/1775913/4863162.aspx/1?Re+Order+by+navigation+properties+Re: Order by navigation properties <p></p> <blockquote><span class="icon-blockquote"></span> <h4>Goraleye</h4> <p></p> <p>But I will select&nbsp;<span>select p.Category &nbsp;and not &nbsp;<span>select p.Category.name</span></span></p> <p><span><span>Because I need other properties of Category.</span></span></p> <p></p> </blockquote> <p></p> <p>Hello again</p> <p>If you can get the instance of Category from ProductYou should get other public properties from Category instance</p> 2012-03-05T00:22:13-05:004874850http://forums.asp.net/p/1775913/4874850.aspx/1?Re+Order+by+navigation+properties+Re: Order by navigation properties <p>Hi this does not work</p> <p>Because I've list of categories and I would like to&nbsp; order by product.dtcreation and return again my list of categories.</p> <p>For sample :&nbsp; <span id="result_box" lang="en"><span class="hps">Category</span> <span class="hps">with</span> <span class="hps">product</span>&nbsp; <span class="hps"> creation date</span> at <span class="hps">02/01/2012</span> must be displayed&nbsp;<span class="hps">after&nbsp; <span id="result_box" lang="en"><span class="hps">Category</span> <span class="hps"> with</span> <span class="hps">product</span>&nbsp; <span class="hps">creation date</span> at&nbsp;</span></span><span class="hps"> 01/01/2012</span></span></p> <pre class="prettyprint">var result = (from c in listCategories orderby c.Products.OrderByDescending(p =&gt; p.DtCreation) select c);</pre> 2012-03-12T08:35:15-04:004874872http://forums.asp.net/p/1775913/4874872.aspx/1?Re+Order+by+navigation+properties+Re: Order by navigation properties <p></p> <blockquote><span class="icon-blockquote"></span> <h4>Goraleye</h4> <p></p> <p>Hi this does not work</p> <p>Because I've list of categories and I would like to&nbsp; order by product.dtcreation and return again my list of categories.</p> <p>For sample :&nbsp; <span id="result_box" lang="en"><span class="hps">Category</span> <span class="hps">with</span> <span class="hps">product</span>&nbsp; <span class="hps"> creation date</span> at <span class="hps">02/01/2012</span> must be displayed&nbsp;<span class="hps">after&nbsp; <span id="result_box" lang="en"><span class="hps">Category</span> <span class="hps"> with</span> <span class="hps">product</span>&nbsp; <span class="hps">creation date</span> at&nbsp;</span></span><span class="hps"> 01/01/2012</span></span></p> <pre class="prettyprint">var result = (from c in listCategories orderby c.Products.OrderByDescending(p =&gt; p.DtCreation) select c);</pre> <p></p> </blockquote> <p></p> <p>You mean you cannot get c as Category</p> 2012-03-12T08:47:28-04:004874925http://forums.asp.net/p/1775913/4874925.aspx/1?Re+Order+by+navigation+properties+Re: Order by navigation properties <p>No,</p> <p>I can get C as Category but the oder by does not return a good shorted list categories</p> <p>&nbsp;</p> 2012-03-12T09:10:15-04:004874940http://forums.asp.net/p/1775913/4874940.aspx/1?Re+Order+by+navigation+properties+Re: Order by navigation properties <p></p> <blockquote><span class="icon-blockquote"></span> <h4>Goraleye</h4> <p></p> <p>No,</p> <p>I can get C as Category but the oder by does not return a good shorted list categories</p> <p></p> </blockquote> <p></p> <p>What do u wantYou want to sort Category by Products</p> 2012-03-12T09:14:20-04:00