Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将此SQL转换为C#中的LINQ?_C#_Linq - Fatal编程技术网

如何将此SQL转换为C#中的LINQ?

如何将此SQL转换为C#中的LINQ?,c#,linq,C#,Linq,我在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

我在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 

我可以得到这个的输出。现在我的问题是如何使用Entity Framework实现此查询?

我知道这不是对您问题的直接回答,但是一种方法是在SQL中创建一个存储过程,然后在Entity Framework中调用该存储过程

代码优先:

数据库优先:
假设您有以下型号:

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);

你的问题在哪里?我现在编辑。请查一下是的,扎普诺基卡。我通过存储过程来实现这一点,然后您需要做的就是创建一个数据库—数据库的第一个模型。然后,当您实例化
Db Context
的新实例时,您可以转到
dbContext.StoredprocedureName
谢谢ivan。让我试试这个解决方案