将SQL Server查询转换为具有ROW_NUMBER()和OVER的C#LINQ

时间:2012-04-26 16:56:58

标签: c# sql sql-server linq row-number

我需要将此查询转换为C#LINQ 但我不知道如何开始。谢谢你的时间。

SELECT s.TextId, s.Title, s.CategoryId, s.Name, s.DateSent, Row
FROM 
   (SELECT t.TextId, t.Title, t.CategoryId, c.Name, t.DateSent, 
        ROW_NUMBER() OVER (PARTITION BY t.CategoryId ORDER BY t.datesent DESC) AS Row
    FROM Concept_Text t 
    JOIN Concept_Text_Categories c 
      ON t.CategoryId = c.CategoryId 
    JOIN Concept_Text_CategoryToPlugin cp 
      ON c.CategoryId = cp.CategoryId 
    JOIN Concept_Text_Plugins p 
      ON cp.PluginId = p.PluginId
    WHERE p.type = 12 AND (t.IsPublished = 'True') AND (Visible = 'True')
    GROUP BY t.TextId, t.Title, t.CategoryId, c.Name, t.DateSent) s
WHERE Row <=12

在帮助下,到目前为止,我得到了这个

(from t in Concept_Text
   join c in Concept_Text_Categories on t.CategoryId equals c.CategoryId
   join cp in Concept_Text_CategoryToPlugin on c.CategoryId equals cp.CategoryID
   join p in Concept_Text_Plugins on cp.PluginID equals p.PluginID
   where p.Type == 12 && t.IsPublished && t.Visible
   group cp by new { t.TextId, t.Title, t.CategoryId, c.Name, t.DateSent } into gr
   orderby gr.Key.DateSent descending
   select new
   {
       gr.Key.TextId,
       gr.Key.Title,
       gr.Key.CategoryId,
       gr.Key.Name,
       gr.Key.DateSent
   })

现在唯一的问题是我们需要获得每个类别的12个企业。

2 个答案:

答案 0 :(得分:1)

我认为是这样的:

var query = 
(
   from t in Concept_Texts
   join c in Concept_Text_Categories on t.CategoryId equals c.CategoryId
   join cp in Concept_Text_CategoryToPlugin on c.CategoryId equals cp.CategoryId
   join p in Concept_Text_Plugins on cp.CategoryId equals p.CategoryId

   where p.type = 12 && t.IsPublished == "True" AND t.Visible == "True"
   group cp by new {t.TextId, t.Title, t.CategoryId, c.Name, t.DateSent} into gr
   select new {
              gr.Key.TextId, 
              gr.Key.Title, 
              gr.Key.CategoryId, 
              gr.Key.Name, 
              gr.Key.DateSent,
              MinC = gr.Min(gcp=>gcp.CategoryId },
              MaxC = gr.Max(gcp=>gcp.CategoryId }
).Where(c=>c.CategoryId >= c.MinC && c.CategoryId <= c.MaxC)
 .OrderByDescending(c=>c.DateSent)
 .Skip(0).Take(12);

你能用LINQ Pad测试吗?看看SQL会产生什么?

答案 1 :(得分:1)

(from c in Concept_Text_Categories
                            join cp in Concept_Text_CategoryToPlugin
                            on c.CategoryId equals CategoryID
                            join p in Concept_Text_Plugins
                            on PluginID equals p.PluginID
                            where p.Type == 12
                            && c.Enabled
                            && p.Enabled
                            orderby c.Name ascending
                            select new ()
                            {
                                CategoryId = c.CategoryId,
                                Name = c.Name,
                                Materias = (from t in Concept_Text
                                        where t.CategoryId == c.CategoryId
                                        && t.IsPublished
                                        && t.Visible
                                        orderby t.DateSent
                                        select new ()
                                        {
                                            TextId = t.TextId,
                                            Title = t.Title
                                        }).Take(12)
                            })