2

I'm trying to order by an attribute of a related object. Just an example, this is the situation. I have this two classes:

public class Publication
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Product> Products { get; set; }
}

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int PublicationId { get; set; }
    [ForeignKey("PublicationId")]
    public Publication Publication { get; set; }
    public int Category { get;set; }
}

Now I need to get all the Publications with Product's Category = 1 and ordered by Publication Id desc:

IQueryable<Publication> query = db.Publications.Where("Products.Any(Category = 1)");
List<Publication> list = query.OrderBy("Id desc").ToList();

This is working great! But... How can I order Publications by Product's category (desc or asc) using Dynamic Linq?

Martín
  • 3,105
  • 7
  • 25
  • 43
  • Can't you just do OderByDescending? (https://msdn.microsoft.com/en-us/library/bb534861(v=vs.110).aspx) – Clueless Dec 10 '16 at 22:03
  • The order (asc or desc) is not the problem now. The problem is about how to order (in asc or desc) by a relation. In this case Category (from Product) – Martín Dec 10 '16 at 22:22
  • 1
    `Publication` has one or more products and, hence, categories. You first should define how want to sort collections of categories. – Gert Arnold Dec 10 '16 at 22:32
  • A Publication has one or more Products. Category is just an integer into Product. I just need to sort PUBLICATIONS based on the number of the Product category. – Martín Dec 10 '16 at 22:35
  • related? http://stackoverflow.com/questions/6617596/how-do-i-sort-a-collection-with-child-property – J. Pichardo Dec 10 '16 at 22:36
  • Yes but i don't want to order the Products inside the Publication. I want to order de Publications based on the children Product Category Id. – Martín Dec 10 '16 at 22:39
  • Martin, what @GertArnold meant is that **one** Publication has **many** Products, thus categories. You incorrectly state "by product category" becase there is no single product category per publication. So you need to define what do you mean to sort an object by a property of a **collection** of related objects. – Ivan Stoev Dec 11 '16 at 17:12

1 Answers1

0

I haven't found how to do it using System.Linq.Dynamic but as for Linq this works

var query = publications.OrderByDescending(p => p.Products.Select(x => x.Category).FirstOrDefault()).ToList();

Here is the test class:

var publications = new List<Publication>
        {
            new Publication
            {
                Products = new List<Product>
                {
                    new Product {Category = 5},
                    new Product {Category = 6},
                    new Product {Category = 7}
                }
            },
            new Publication
            {
                Products = new List<Product>
                {
                    new Product {Category = 2},
                    new Product {Category = 3},
                    new Product {Category = 4}
                }
            },
            new Publication
            {
                Products = new List<Product>
                {
                    new Product {Category = 8},
                    new Product {Category = 9},
                    new Product {Category = 10}
                }
            }
        };

        var query = publications.OrderByDescending(p => p.Products.Select(x => x.Category).FirstOrDefault()).ToList();

        query.ForEach(x => Console.WriteLine(x.ToString()));

The output was:

8 , 9 , 10
5 , 6 , 7
2 , 3 , 4

As you see it doesn't internally order the Products.

J. Pichardo
  • 3,077
  • 21
  • 37
  • Thank you but I need this in Dynamic Linq. – Martín Dec 11 '16 at 06:28
  • Ok, I'll see to it, what is your order criteria?, But just out of curiosity, why Dynamic Linq if you can use the "normal" one? – J. Pichardo Dec 11 '16 at 06:29
  • That's beacause I'm building the string query from url in webapi. This is all made dynamically. Everything it's working okay. I just need this in order to complete the project. The order you made is okay. Publications ordered asc or desc based on the nested Product Category. – Martín Dec 11 '16 at 06:36
  • The way I did it is being ordered by the category of the first product found, if there is no then it takes 0, is that your criteria? – J. Pichardo Dec 11 '16 at 06:48
  • I think it's okay – Martín Dec 11 '16 at 14:39
  • What if one product has [2,3,4] and the next has [3,4,2]? Ordering by the first category is next to random. – Gert Arnold Dec 11 '16 at 19:21
  • Exactly, that's why I wanted to know to criteria. – J. Pichardo Dec 11 '16 at 19:55