[英]How to convert this SQL to LINQ in C#?
我在SQL查詢中有一個查詢,如下所示:
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
我可以得到這個輸出。 現在我的問題是如何使用實體框架實現此查詢?
我知道這不是您問題的直接答案,但是一種方法是在SQL中創建一個存儲過程,然后在Entity Framework中調用該存儲過程。
代碼優先: 如何在Entity Framework 6中調用存儲過程(代碼優先)?
數據庫優先: https : //msdn.microsoft.com/zh-cn/data/gg699321.aspx
假設您具有以下模型:
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.