2012-04-26 17 views
0

このクエリをC#LINQ に変換する必要がありますが、どのように開始するのか分かりません。あなたの時間をありがとう。SQL ServerクエリをROW_NUMBER()とOVERを持つ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 enteriesを取得する必要がありました。

+2

通常、ページネーションに使用されます。 LINQでは、SkipとTakeメソッドを使用できます。あなたはこれらの何かを試しましたか? –

+0

@AdrianIftode、実際には各カテゴリの最新の12のエントリーが必要ですが、結果はこの写真のようなものです[リンク](http://img252.imageshack.us/img252/8241/stackoverflow.png) –

+1

Can not LINQに特定のクエリを使用させますか?私はそれがストアドプロシージャを呼び出すことができます知っている。これは、複雑なクエリを実行する最善の方法かもしれません。 – HLGEM

答えて

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) 
          }) 
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パッドでこれをテストすることができますか? SQLが生成するものを参照してください。

+0

私はクエリにいくつかの変更を加えたので、結果は[img](http://img692.imageshack.us/img692/8241/stackoverflow.png) –

+0

です。私は生成されたSQLを参照してください? –

+0

私は親切に迷っています(MinCとMaxCの間のs.CategoryId)。あなたはこれについての答えが得られない場合、あなたは@ HLGEMのソリューション –

関連する問題