繁体   English   中英

如何声明匿名类型列表

[英]How to declare List of anonymous type

在此官方ASP.NET MVC Core教程中,他们定义了如下ViewModel,其中Movie是类。 然后在Index(...)方法中(在本教程结束时,他们从一个简单的LINQ查询中填充ViewModel,如下面的Controller中所示):

ViewModel

public class MovieGenreViewModel
{
    public List<Movie> movies;
    public SelectList genres;
    public string movieGenre { get; set; }
}

控制器

public async Task<IActionResult> Index(string movieGenre, string searchString)
{
    // Use LINQ to get list of genres.
    IQueryable<string> genreQuery = from m in _context.Movie
                                    orderby m.Genre
                                    select m.Genre;

    var movies = from m in _context.Movie
                 select m;

    if (!String.IsNullOrEmpty(searchString))
    {
        movies = movies.Where(s => s.Title.Contains(searchString));
    }

    if (!String.IsNullOrEmpty(movieGenre))
    {
        movies = movies.Where(x => x.Genre == movieGenre);
    }

    var movieGenreVM = new MovieGenreViewModel();
    movieGenreVM.genres = new SelectList(await genreQuery.Distinct().ToListAsync());
    movieGenreVM.movies = await movies.ToListAsync();

    return View(movieGenreVM);
}

问题 :在上述ViewModel中,属性影片的类型为List<movie> ,而Index(...)方法中使用的查询是仅包含一个表(影片)的简单查询。 但是,如果查询涉及具有两个表的INNER JOIN并因此返回匿名类型的对象,如下所示,该怎么办? 在那种情况下,我们如何在ViewModel中声明匿名类型List<.?.> ,然后在ViewModel中填充List <。?。>类型的属性? 在下面的示例中,我使用List<dynamic>List<object>定义了一个ViewModel,并尝试了myViewModel.list = leftOuterJoinQuery.ToListAsync()但由于Cannot implicitly convert type 'List<<anonymous type: int CategoryId, string CategoryName, string ProdName>>>' to 'System.Collections.Generic.List<dynamic>而收到错误Cannot implicitly convert type 'List<<anonymous type: int CategoryId, string CategoryName, string ProdName>>>' to 'System.Collections.Generic.List<dynamic>

LINQ INNER JOIN查询

var innerJoinQuery =
    from category in categories
    join prod in products on category.ID equals prod.CategoryID
    select new { ProductName = prod.Name, Category = category.Name }; //produces flat sequence

更新

如下@StephenMuecke所建议,可以通过创建另一个包含我的查询返回的属性的视图模型来解决该问题。 但是我在考虑是否有办法避免创建额外的视图模型。

有一种无需创建新VM即可完成此操作的方法,但是我不确定这是正确的方法。

首先,我将如下所示切换虚拟机

public class MovieGenreViewModel
{
    public ICollection movies;
    public SelectList genres;
    public string movieGenre { get; set; }
}

定义一个辅助方法来组成匿名对象的列表

public static List<T> CreateList<T>(params T[] elements)
{
     return new List<T>(elements);
}

最后,更改Index()方法内的行

movieGenreVM.movies = await movies.ToListAsync();

   var output = await innerJoinQuery.ToListAsync();
   movieGenreVM.movies = CreateList(output);

希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM