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.
source share