LINQ is an anonymous type, not providing the list I need

I execute a LINQ query to call the object Recipethat needs to be ordered by its result. In the beginning I have a IEnumberabletype Recipe(already filtered with search criteria) calledselectedRecipies

Then, with the help of my google friend, I fulfilled this request using an anonymous type:

var finalQuery = ((from h in db.StarRatings
                       where selectedRecipies.Any(sr => sr.IDRecipe == h.IDRecipe)
                     group h by new { h.IDRecipe } into hh
                     select new
                     {
                         hh.Key.IDRecipe,
                         Score = hh.Sum(s => s.Score)
                     }).OrderByDescending(i => i.Score));

And I think this works ... My problem is that for my view, I need to have a type Recipeand finalQuery, it seems, a type IEnumerable<'a>, where ais an anonymous type ...

How can I get a List<>recipe type without breaking OrderByDescending?

+5
source share
2 answers

RecipeViewModel ( RecipeDto), :

select new RecipeViewModel
 {
     hh.Key.IDRecipe,
     Score = hh.Sum(s => s.Score)
 }).OrderByDescending(i => i.Score));

,

, ( ) Recipe . , , , . , - Recipe, Score:

from r in db.Recipes
where // .....  (do your filtering here)
select new RecipeViewModel
  {
      Id = r.Id,
      // ... more recipe properties
      Score = r.StarRatings.Sum(rating => rating.Score)
  }

, Recipe.StarRatings. , join . ( ).

+2

Recipe:

select new Recipe // Use constructor or object initiailizer here
                 {
                     ID = hh.Key.IDRecipe,
                     Score = hh.Sum(s => s.Score)
                 }).OrderByDescending(i => i.Score))
                 .ToList(); // To make your List<T>
+2