clinton
clinton

Reputation: 132

Linq sort by products but display cateogires

So I have an issue where the code is Selecting a Category and I need to change sort order to sort by product.Name Then By category.name.

But the problem is I still want to select a category, but how do I sort by product.name first without adding an extra join or select.

from category in categories select category.name orderby category.Name //orderby category name

Later on in view I loop foreach(category.products) and pass in category.product[i] to view to display

But sort order is wrong, order is always by Category.Name How do I sort by Product.Name first and then by Category.Name? Will SelectMany help? Again I do not want to disrupt the select part of my query, just the Order by stuff.

class Product { public string Name { get; set; } public int CategoryID { get; set; } }

class Category { public string Name { get; set; } public int ID { get; set; } }

// Specify the first data source. static List categories = new List() { new Category(){Name="Beverages", ID=001}, new Category(){ Name="Condiments", ID=002}, new Category(){ Name="Vegetables", ID=003}, new Category() { Name="Grains", ID=004}, new Category() { Name="Fruit", ID=005}
};

// Specify the second data source. static List products = new List() { new Product{Name="Cola", CategoryID=001}, new Product{Name="Tea", CategoryID=001}, new Product{Name="Mustard", CategoryID=002}, new Product{Name="Pickles", CategoryID=002}, new Product{Name="Carrots", CategoryID=003}, new Product{Name="Bok Choy", CategoryID=003}, new Product{Name="Peaches", CategoryID=005}, new Product{Name="Melons", CategoryID=005}, };

Upvotes: 0

Views: 204

Answers (3)

clinton
clinton

Reputation: 132

class Category
{
    public string Name { get; set; }
    public int ID { get; set; }
    public List<Product> products { get; set;}
}

class Product 
{
    public string Name { get; set; }
    public int ID { get; set; }
}


void Main()
{
        // Specify the second data source.
         List<Product> BevProducts = new List<Product>()
        {
            new Product{Name="ZCola"},
        };

        // Specify the third data source.
         List<Product> CondProducts = new List<Product>()
        {
            new Product{Name="Sugar"},
        };
      // Specify the first data source.
     List<Category> categories = new List<Category>()
        { 
            new Category(){Name="Beverages", ID=001, products=BevProducts},
            new Category(){ Name="Condiments", ID=002, products=CondProducts},

        };



        var sortedCats = categories.OrderBy(c => c.products.Min(p => p.Name)).ToList();

        foreach (var category in sortedCats)
        {
            //display category
            System.Console.Out.WriteLine(category.Name);


            //Assuming each category contains exactly one product in the list ie 1 to 1 relationship
            // how can I sort by product.Name, so if ZCola comes before Sugar, ZCola's Category (Beverages) sorts before Condiments
            // so in this Case Beverages, Condiments is the right order, because ZCola comes after Sugar.
            var  sortedProductsPerCategory = category.products.OrderBy(p => p.Name).ToList();

            foreach (var product in sortedProductsPerCategory)
            {
                    //display product
                    System.Console.Out.WriteLine("   " + product.Name);
            }
        }



} 

Upvotes: 0

clinton
clinton

Reputation: 132

//LinqPad code

class Category
{
    public string Name { get; set; }
    public int ID { get; set; }
    public List<Product> products { get; set;}
}

class Product 
{
    public string Name { get; set; }
    public int ID { get; set; }
}


void Main()
{
        // Specify the second data source.
         List<Product> BevProducts = new List<Product>()
        {
            new Product{Name="ZCola"},
        };

        // Specify the third data source.
         List<Product> CondProducts = new List<Product>()
        {
            new Product{Name="Sugar"},
        };
      // Specify the first data source.
     List<Category> categories = new List<Category>()
        { 
            new Category(){Name="Beverages", ID=001, products=BevProducts},
            new Category(){ Name="Condiments", ID=002, products=CondProducts},

        };



        var sortedCats = categories.OrderBy(c => c.ID).ToList();

        foreach (var category in sortedCats)
        {
            //display category
            System.Console.Out.WriteLine(category.Name);

        //Assuming each category contains exactly one product in the list ie 1 to 1 relationship
// how can I sort by product.Name, so if ZCola comes before Sugar, ZCola's Category (Beverages) sorts before Condiments
// so in this Case Beverages, Condiments is the right order, because ZCola comes after Sugar.
            var  sortedProductsPerCategory = category.products.OrderBy(p => p.Name).ToList();

            foreach (var product in sortedProductsPerCategory)
            {
                    //display product
                    System.Console.Out.WriteLine("   " + product.Name);
            }
        }



} 

Upvotes: 0

user3956566
user3956566

Reputation:

Oh, I saw you query, it was badly formatted.

You need to order by Product.Name group by Category.Name

Upvotes: 0

Related Questions