查询从sql转换为linq

时间:2013-08-19 14:22:30

标签: asp.net sql-server linq sql-server-2008

我想将以下SQL查询转换为LINQ

SELECT a.* 
FROM   campaign_newslettersubscriber_track a 
       INNER JOIN (SELECT DISTINCT campaignid, 
                                   Min(id) AS id 
                   FROM   campaign_newslettersubscriber_track 
                   GROUP  BY campaignid) AS b 
               ON a.campaignid = b.campaignid 
                  AND a.id = b.id 

2 个答案:

答案 0 :(得分:0)

 var subquery = from c in objectContext.campaign_newslettersubscriber_track 
                         group c by c.campaignid into g
                         select new
                         {
                             id = g.Min(x => x.campaignid),
                             campaignId = g.Key
                         };

var n = from c in objectContext.campaign_newslettersubscriber_track 
        join cin in subquery.Distinct() on c.campaignid equals cin.campaignId
        select c;

答案 1 :(得分:0)

我会研究名为Linqer的工具。

可以使用T-SQL并将其转换为LINQ,LINQ to SQL(使用SqlMetal)或LINQ to Entities(使用EdmGen)。

请注意,这不是一个免费工具(一个许可证为60.00美元,在撰写本文时更便宜,并且不会转换所有T-SQL函数),这里是受支持的functions列表