0

我需要将此查询转换为 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 个条目。

4

2 回答 2

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 产生了什么?

于 2012-04-26T17:28:58.270 回答
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)
                            })
于 2012-05-07T12:57:41.687 回答