Linq To Sql select命令

时间:2011-11-28 14:58:40

标签: c# asp.net sql-server linq-to-sql

你能帮我把这个select命令更改为linq to sql select命令吗?

string value = ddl.SelectedValue;

SELECT  * 
FROM    Song 
WHERE   SongID IN (
            SELECT SongID 
            FROM   AlbumSong 
            WHERE  AlbumID = @value)

2 个答案:

答案 0 :(得分:1)

这是linq方法,更简洁dot notation

var dataContext = new YouDataContext();
int value = 10;
dataContext.Song.Where(a => 
     dataContext.AlbumSong.Where(alSong => alSong.AlbumId == value)
                          .Select(alSong => alSong.SongId).Contains(a.SongId));

答案 1 :(得分:0)

你有:

"SELECT * FROM Song WHERE SongID in (SELECT SongID FROM AlbumSong WHERE AlbumID = " + value + ")"

你有没有在这里使用连接而不是嵌套选择的原因?像:

"SELECT * FROM Song INNER JOIN AlbumSong ON Song.SongId = AlbumSong.SongId WHERE AlbumID = " + value

或者我在这里误解了你的需求而加入是不合适的?