views:

39

answers:

3
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?
Thanks!

+1  A: 

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.

Matti Virkkunen
+2  A: 

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()
            };
Ahmad Mageed
+1 was just writing that
Oskar Kjellin
It works... and this other query version is just what I needed! Thanks!
ile
+1  A: 

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);
p.campbell
yep... Ahmad already gave me answer: http://stackoverflow.com/questions/2556378/where-to-insert-orderby-expression-in-this-linq-to-sql-query/2556397#2556397 ...thanks!
ile