排序从另一个列表中ID的列表 [英] Sort a list from another list IDs

查看:93
本文介绍了排序从另一个列表中ID的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些像这样的标识符的列表:

 列表<长> docIds =新的List<长>(){6,1,4,7,2}; 



Morover,我有<的另一份名单; T> 项目,由上述的IDS为代表

 列表< T>文档= GetDocsFromDb(...)



我需要保持相同的顺序在两个集合,使在的项目列表与LT; T> 必须在比第一个(因搜索引擎得分的原因)相同的位置。这个过程不能在 GetDocsFromDb()函数来完成。



如果有必要,有可能改变第二份名单到一些其他的结构(词典<长,T> 的例子),但我不想去改变它。



有没有什么简单有效的方法做LINQ这个根据一些标识ordenation?


解决方案

 文档= docs.OrderBy(D => docsIds.IndexOf(d.Id) ).ToList(); 


I have a list with some identifiers like this:

List<long> docIds = new List<long>() { 6, 1, 4, 7, 2 };

Morover, I have another list of <T> items, which are represented by the ids described above.

List<T> docs = GetDocsFromDb(...)

I need to keep the same order in both collections, so that the items in List<T> must be in the same position than in the first one (due to search engine scoring reasons). And this process cannot be done in the GetDocsFromDb() function.

If necessary, it's possible to change the second list into some other structure (Dictionary<long, T> for example), but I'd prefer not to change it.

Is there any simple and efficient way to do this "ordenation depending on some IDs" with LINQ?

解决方案

docs = docs.OrderBy(d => docsIds.IndexOf(d.Id)).ToList();

这篇关于排序从另一个列表中ID的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆