Reputation: 1
I am coming from Perl doing first steps with C# and Linq. My question is if there is a way to put parts of a Linq statement in a variable like this (Code only for short illustration, not tested) to use it several times more easily and flexible than writing it new every time. Also one could use different Order-Statements (Select-Case, Array) for the same MQuery.
var orderstatement = "orderby m.Reihenfolge ascending, m.Datum descending, m.Titel ascending";
var MQuery = (from m in _context.Movie
orderstatement
where m.Sichtbar == true && m.Gesperrt == false
select m;
Thanks for advising.
Upvotes: 0
Views: 94
Reputation: 337
You can try something like this with extension methods. First, define the order by movies method
public static IQueryable<Movie> OrderMovies(this IQueryable<Movie> movies) {
return movies.OrderBy(m => m.Reihenfolge).ThenByDescending(m => m.Datum).ThenBy(m => m.Title);
}
And then use it in the movies query
var movies = _context.Movies.Where(m => m.Sichtbar == true && m.Gesperrt == false).OrderMovies().Select(m => m)
Upvotes: 1