[英]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.