How to order items in a LINQ-related sub-collection

I have a SQL Server database table called Category. And another database table called SubCategory.

SubCategory relates a foreign key to a category. Because of this, thanks to LINQ, each Cateogory has a subcategory property, and LINQ is good enough to return all the subcategories associated with my category when I grab it.

If I want to sort the categories alphabetically, I can simply do:

return db.Categories.OrderBy(c => c.Name); 

However, I do not know how to organize the collection of SubCategories within each category.

My goal is to return a collection of categories, where all collections of subcategories within them are sorted alphabetically by name.

+4
source share
4 answers

In such cases, I prefer to use extension methods - because they do a great job of encapsulating this logic for reuse, and a good name can also help to easily find the logic.

For example, in projects in which I used L2S in MVC applications, I simply create a folder in the Mods folder called Extensions, and then change the extension methods there to the L2S model / class, etc. (just to keep things organized).

Then for this case, I would create something like this:

 public static class CategoryExtensions { public static List<SubCategory> GetAlphabetizedSubCategories(this Category category) { return category.SubCategories .OrderBy(sc => sc.Name) .ToList(); } } 

And then, from my MVC view, for example, if I go into the category as Model, I could just do something simple:

 <% foreach(SubCategory subCat in Model.GetAlphabetizedSubCategories()) { %> <p>Stuff goes here... </p> <% } %> 

This is an example of MVC - but you can apply it almost everywhere, since the idea is that you "decorate" your object with a method that explicitly does what you need, and you do not make additional copies /, etc.

+2
source

You need to use Group By in categories and then order by subcategory.

0
source

You can do this by converting linq objects to lists.

eg.

 var categoryList = db.Categories .OrderBy(c=> c.Name) .ToList(); categoryList.ForEach(c => c.SubCategory.ToList().Sort((x, y) => x.Name.CompareTo(y.Name))); return categoryList; 

Listing categories allows you to use the Linq ForEach extension method. Converting a SubCategory property to a list gives you an inline sorting method.

0
source

Here is one possible way to do this:

In the dbml designer, select the relationship (dashed line) between Category and Subcategory .

Open the properties panel and expand the "Child property".

Change the name from SubCategories to _subCategories and access from Public to Internal .

Click on the design surface and press F7 or right-click and select View Code.

Add this property:

 public partial class Category { public IOrderedEnumerable<SubCategory> SubCategories { get { return _subCategories.OrderBy(s => s.Name); } } } 

You now have the same SubCategories property that is really ordered.

0
source

Source: https://habr.com/ru/post/1310443/


All Articles