如何组合多个LINQ表达式

时间:2014-03-26 06:49:48

标签: c# .net linq

如何将以下LINQ语句合并为一个语句

var albums = db.Albums.Include(a => a.Artist).Include(a => a.Genre);
                var albumByGenre = from a in albums
                                   where a.GenreId == GenreId
                                   orderby a.AlbumId descending
                                   select a;

                List<Album> albumList =  albumByGenre.ToList();

1 个答案:

答案 0 :(得分:2)

这里结合了wersion和函数语法

var albumList = db.Albums.Include(a => a.Artist).Include(a => a.Genre) //1st part
    .Where(a=>a.GenreId==GenreId).OrderByDescending(x=>x.AlbumId) //2nd part
    .ToList(); //3rd part

您可以将查询链接到一个大查询