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
使用LINQ获取记录的历史记录_Linq_Entity Framework_History - Fatal编程技术网

使用LINQ获取记录的历史记录

使用LINQ获取记录的历史记录,linq,entity-framework,history,Linq,Entity Framework,History,我正在使用带有存储库模式和工作单元对象的实体框架 我有一个属性为“RequestId”、“OldRequestId”的实体请求,可以使用requestRepository对象访问该请求 例如:requestRepository.GetAll(),requestRepository.GetFiltered(r=>r.Requestid=10) 如果我传递一个RequestId,它应该检索特定的记录 如果OldRequestId在检索到的记录中不为null,那么它也应该带来旧请求数据 它应该一直持

我正在使用带有存储库模式和工作单元对象的实体框架

我有一个属性为“RequestId”、“OldRequestId”的实体请求,可以使用requestRepository对象访问该请求

例如:requestRepository.GetAll(),requestRepository.GetFiltered(r=>r.Requestid=10)

  • 如果我传递一个RequestId,它应该检索特定的记录
  • 如果OldRequestId在检索到的记录中不为null,那么它也应该带来旧请求数据
  • 它应该一直持续到OldRequestId为null

简单的方法如下:

public static IEnumerable<Data> GetRecursive(int id)
{
    while (true)
    {
        var tmp = GetFiltered(x => x.Requestid == id);
        yield return tmp;

        if (tmp.OldRequestId.HasValue)
            id = tmp.OldRequestId.Value;
        else
            yield break;
    }
}
公共静态IEnumerable GetRecursive(int-id)
{
while(true)
{
var tmp=GetFiltered(x=>x.Requestid==id);
收益tmp;
if(tmp.OldRequestId.HasValue)
id=tmp.OldRequestId.Value;
其他的
屈服断裂;
}
}

请注意,此代码将运行以对数据库进行多个查询。性能不是最好的,但它可能适合您的场景。

简单的方法如下:

public static IEnumerable<Data> GetRecursive(int id)
{
    while (true)
    {
        var tmp = GetFiltered(x => x.Requestid == id);
        yield return tmp;

        if (tmp.OldRequestId.HasValue)
            id = tmp.OldRequestId.Value;
        else
            yield break;
    }
}
公共静态IEnumerable GetRecursive(int-id)
{
while(true)
{
var tmp=GetFiltered(x=>x.Requestid==id);
收益tmp;
if(tmp.OldRequestId.HasValue)
id=tmp.OldRequestId.Value;
其他的
屈服断裂;
}
}

请注意,此代码将运行以对数据库进行多个查询。性能可能不是最好的,但它可能适合您的场景。

我相信您会希望为此使用递归CTE。这在EF中不受直接支持(据我所知),但请看一看:我相信您会希望为此使用递归CTE。这在EF中没有直接支持(据我所知),但请看一下: