Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.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
C# Linq选择组层次结构中的最新版本_C#_.net_Linq_Asp.net Mvc 4 - Fatal编程技术网

C# Linq选择组层次结构中的最新版本

C# Linq选择组层次结构中的最新版本,c#,.net,linq,asp.net-mvc-4,C#,.net,Linq,Asp.net Mvc 4,如果存在自引用层次结构,我想获取每个组的最新记录 我的桌子看起来像这样: RecordId CreatedDate ParentRecordId 1 2012/05/13 NULL 2 2012/05/13 NULL 3 2012/05/14 1 4 2012/05/15 3 我只想选择最新版本的记录。 所以在本例中,我只想选择RecordId=2和Rec

如果存在自引用层次结构,我想获取每个组的最新记录

我的桌子看起来像这样:

RecordId CreatedDate ParentRecordId
   1     2012/05/13       NULL   
   2     2012/05/13       NULL   
   3     2012/05/14        1     
   4     2012/05/15        3     
我只想选择最新版本的记录。
所以在本例中,我只想选择RecordId=2和RecordId=4

这是我到目前为止所拥有的,我被卡住了

     db.Records
       .Where(x => x.ParentRecordId!= null).GroupBy(x => x.ParentRecordId)
       .SelectMany(x=>x.OrderByDescending(y=>y.CreatedDate).Take(1)).ToList();

一个回答“获取所有条目,其中没有另一个条目将我视为父条目”的查询如何。这听起来是一样的,除非我误解了:

db.Records.Where(x => !db.Records
    .Select(r => r.ParentRecordId).Contains(x.RecordId))

然而,我对你所说的“圆形”有点困惑。层次结构怎么可能是循环的?

我的左连接有点缺乏,但类似的东西应该可以做到这一点

var query = from r1 in db.Records
            join r2 in db.Records
                on r1.RecordId equals r2.ParentRecordId into rec
            from r in rec.DefaultIfEmpty()
            where r == null
            select r1;

您应该首先获得
ParentRecordId
s的列表,然后检查
RecordId
是否在该列表中,如果在该列表中,我们应该将其从结果中排除:

var parentIds = db.Records
                .Where(r => r.ParentRecordId != null)
                .Select(r => r.ParentRecordId)
                // this ToList call may increase performance. Depending 
                // on the size of the result set we may not want to call
                // the database every time for this info
                .ToList();
var wantedRecords = from r in db.Records
                    where !parentIds.Contains(r.RecordId)
                    select r;

OP只想要最新版本的记录,其中有一个group by和Take 1
,然而,我对你所说的“循环性”有点困惑。层次结构怎么可能是循环的?
根据其定义,层次结构不可能是循环的。但是,自引用表可能不是层次结构。项目1可以是项目2的父项,项目2可以是项目1的父项。这将是一个循环,因此不再是一个有效的层次结构。我对OP想要什么感到困惑,因为我认为他的代码对于1解释是正确的。也许如果他更新了一个例子,说明他当前的代码是如何工作的,我会更好地理解这个问题。太棒了,这很好,但是我如何确保记录是组中最新的?我只需要添加orderbydescending吗?@user194076它会给你记录2和4,它们各自组中的最后一条记录(没有被新记录替换的记录,即没有记录将它们作为父ID)不需要排序,除非你想要按特定顺序排列2和4。应该可以,请注意,如果数据库中有数百万条记录,它将获取它们的所有parentid,只需在查询中将它们全部传回一秒钟即可。@JoachimIsaksson这是一个值得关注的问题。但是,除非有那么多的记录,否则会导致此c#代码使用的内存问题。我相信这两个单独的查询通常会比在语句中生成
的单个查询执行得更快。但是,在Sql Server 2012及更高版本上,这可能不再是事实,因为它对动态查询具有以前版本所没有的性能增强。