Where to insert 'orderby' expression in this linq-to-sql query
var result = db.PhotoAlbums.Select(albums => new PhotoAlbumDisplay
{
AlbumID = albums.AlbumID,
Title = albums.Title,
Date = albums.Date,
开发者_开发技巧 PhotoID = albums.Photos.Select(photo => photo.PhotoID).FirstOrDefault().ToString()
});
Wherever I try to put orderby albums.AlbumID descending
I get error. Someone knows solution?
This should work:
var result = db.PhotoAlbums.Select(albums => new PhotoAlbumDisplay
{
AlbumID = albums.AlbumID,
Title = albums.Title,
Date = albums.Date,
PhotoID = albums.Photos.Select(photo => photo.PhotoID).FirstOrDefault().ToString()
})
.OrderByDescending(item => item.AlbumID);
In query syntax:
var result = from albums in db.PhotoAlbums
orderby albums.AlbumID descending
select new PhotoAlbumDisplay
{
AlbumID = albums.AlbumID,
Title = albums.Title,
Date = albums.Date,
PhotoID = albums.Photos.Select(photo => photo.PhotoID).FirstOrDefault().ToString()
};
If you want to use the query syntax, you'll have to start with "from X select" and work from that. In this case it'd be easier to just use the .OrderBy() method to order the results.
Is this what you're looking for?
var result = db.PhotoAlbums.Select(albums => new PhotoAlbumDisplay
{
AlbumID = albums.AlbumID,
Title = albums.Title,
Date = albums.Date,
PhotoID = albums.Photos.Select(photo => photo.PhotoID).FirstOrDefault().ToString()
})
.OrderByDescending(a=>a.AlbumID);
精彩评论