0

I have two tables: Album(AlbumId, AlbumName) and Photo(PhotoId, PhotoName, AlbumId)

I have :

 select top 1 Photo.PhotoId
 from Photo
 where Album.AlbumId=Photo.AlbumId
 order by NEWID()

This is return one record random

I want convert it to Linq to sql. I tried but not success.

3
  • yes.I was tried.I begin study for Linq.Please help me! Commented Jun 8, 2013 at 18:29
  • you need to use linq join. Commented Jun 8, 2013 at 18:47
  • Please show what you tried. Then we know what "not success" means. Commented Jun 8, 2013 at 20:13

2 Answers 2

0

I believe you would need to join the tables. Also, it depends on what language you are using when you want to create a random Guid - my example uses C#. In query syntax, it should be something like:

from p in Photo
join a in Album on a.AlbumId equals p.AlbumId
orderby Guid.NewGuid()
take 1
select p.PhotoId
0

try this:

var photoId = ( from photo in collecPhotos
                join album in collecAlbums on photo.AlbumId equals album.AlbumId
                orderby NEWID()
                select photo.PhotoId).First();

where collecPhotos and collecAlbums are the collection of your Photo and Album entity i.e. data from the tables. so it can also be myDbContext.Photos and myDbContext.Albums where myDbContext is your Linq-to-Sql DataContext.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.