2016-04-20 12 views
2

私は以下などのSQLクエリでクエリを持っている:このSQLをC#でLINQに変換するには?

with pagedetail as 
(
    select 
     c.componentrefid, l.name, c.startdate, 
     ROW_NUMBER() over(PARTITION By c.componentrefid order by c.startdate desc) as rownumber 
    from 
     FM_componentTransaction as c 
    inner join 
     FM_LK_statusinfo as l on c.Statusinforefid = l.refid 
    inner join 
     fm_scriptinfo as s on s.Refid = c.ScriptRefId 
    where 
     s.CustomerInfoRefId = '85629125-7072-4EFE-9201-97E088E126C6' 
) 
select 
    pd.* 
from 
    pagedetail pd 
where 
    pd.rownumber = 1 

私はこれの出力を得ることができます。今私の質問は、Entity Frameworkを使用してこのクエリを実装する方法ですか?

+0

あなたですrのクエリ? –

+0

私は今編集しました。それをチェックしてください – aaroki

答えて

2

私はこれがあなたの質問に直接答えではないことを知っていますが、SQLでストアドプロシージャを作成し、Entity Frameworkでストアドプロシージャを呼び出す方法があります。

コード最初:まず How to call Stored Procedure in Entity Framework 6 (Code-First)?

データベース: https://msdn.microsoft.com/en-us/data/gg699321.aspx

+0

はいzapnologica。私はストアドプロシージャでこれをやっています – aaroki

+0

あなたがする必要があるのは、データベースのデータベースの最初のモデルを作成することだけです。そして、 'Db Context'の新しいインスタンスをインスタンス化すると、' dbContext.StoredprocedureName'に行くことができます – Zapnologica

1

次のモデルを持っていると仮定すると:

public class ComponentTransaction 
{ 
    public Guid componentrefid { get; set; } 
    public string name { get; set; } 
    public DateTime startdate { get; set; } 
    public Guid Statusinforefid { get; set; } 
    public Guid ScriptRefId { get; set; } 
} 

public class Statusinfo 
{ 
    public Guid refid { get; set; } 
} 

public class Scriptinfo 
{ 
    public Guid refid { get; set; } 
    public Guid CustomerInfoRefId { get; set; } 
} 

コードは次のようになります。

Db db = new Db(); 
Guid customerInfoRefId = new Guid("85629125-7072-4EFE-9201-97E088E126C6"); 
var res = db.ComponentTransactions 
    .GroupBy(c => c.componentrefid) 
    .Select(g => g.OrderByDescending(c => c.startdate).First()) 
    .Join(db.Statusinfos, c => c.Statusinforefid, l => l.refid, (c, l) => c) 
    .Join(db.Scriptinfos.Where(s => s.CustomerInfoRefId == customerInfoRefId), 
     c => c.ScriptRefId, s => s.refid, (c, s) => c); 
+0

ありがとう、ivan。この解決策を試してみましょう – aaroki

関連する問題