我如何编写这个SQL查询到linq查询

时间:2014-01-20 12:38:42

标签: linq-to-sql

我只想使用LinqToSql类查询。在这里,我只想将此sql查询转换为适当的linq查询。

这是我的SQL查询:

SELECT j.[JobID], p.[PreparedEmailID],
        p.[Name] AS 'PreparedEmailName',
        j.[CreatedOn], j.[CompletedOn],
        j.[SubscriberCount], j.[EmailsSent],
        (SELECT TOP 1 [Message] FROM 
        [LoggedMessages] WHERE [JobID] =
        j.[JobID] ORDER BY [LoggedMessageID] DESC)
        AS 'LoggedMessage' FROM [Jobs] AS j 
        INNER JOIN [PreparedEmails] AS p
        ON p.[PreparedEmailID] =
        j.[PreparedEmailID]

我生成的linq查询就像:

var query = from j in db.Jobs
                        join p in db.PreparedEmails on j.PreparedEmailID equals p.PreparedEmailID
                        join l in db.LoggedMessages on j.JobID equals l.JobID into ej
                        from l in ej.DefaultIfEmpty() orderby l.LoggedMessageID descending 
                        orderby l.LoggedMessageID descending
                        orderby j.CreatedOn descending
                        select new
                        {
                            JobID = j.JobID,
                            PreparedEmailID = p.PreparedEmailID,
                            PreparedEmailName = p.Name,
                            CreatedOn = j.CreatedOn,
                            CompletedOn = j.CompletedOn,
                            SubscriberCount = j.SubscriberCount,
                            EmailsSent = j.EmailsSent,
                            LoggedMsg = l.Message
                        };

2 个答案:

答案 0 :(得分:0)

我为你准备了一些linQ查询(但是我没有在VS中测试它,因为我现在无法访问它,所以请小心,因为它可能包含一些错误):

var list = from Jobs
               join PreparedEmails on Jobs.PreparedEmailID == PreparedEmails.PreparedEmailID
               join LoggedMessages on LoggedMessages.JobID == Jobs.JobID
               select
                {
                JobID = Jobs.JobID, 
                       PreparedEmailID = PreparedEmails.PreparedEmailID,
                       PreparedEmailName = PreparedEmails.Name,
                       CreatedOn= Jobs.CreatedOn, 
                       CompletedOn = Jobs.CompletedOn,
                       SubscriberCount = Jobs.SubscriberCount, 
                       EmailsSent = Jobs.EmailsSent,
                       LoggedMessage = LoggedMessages.Message
            } orderby descending LoggedMessages.LoggedMessageID;

它应该有所帮助...

答案 1 :(得分:0)

这是解决方案:

var query = from j in db.Jobs
                        join p in db.PreparedEmails on j.PreparedEmailID equals p.PreparedEmailID
                        orderby j.CreatedOn descending
                        select new
                        {
                            JobID = j.JobID,
                            PreparedEmailID = p.PreparedEmailID,
                            PreparedEmailName = p.Name,
                            CreatedOn = j.CreatedOn,
                            CompletedOn = j.CompletedOn,
                            SubscriberCount = j.SubscriberCount,
                            EmailsSent = j.EmailsSent,
                            LoggedMsg = (from l in db.LoggedMessages
                                         where j.JobID == l.JobID
                                         orderby l.LoggedMessageID descending
                                         select l.Message).FirstOrDefault()
                        };