加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 从另一个数组顺序中排序一个数组?

发布时间:2020-12-16 01:41:39 所属栏目:百科 来源:网络整理
导读:var listOne = new string[] { "dog","cat","car","apple"};var listTwo = new string[] { "car","apple"}; 我需要的是按listTwo中的项目顺序(如果存在)来排序listOne.所以新的清单将按此顺序排列; ???“汽车”,“苹果”,“狗”,“猫” 我想在LINQ中做这个并
var listOne = new string[] { "dog","cat","car","apple"};
var listTwo = new string[] { "car","apple"};

我需要的是按listTwo中的项目顺序(如果存在)来排序listOne.所以新的清单将按此顺序排列;
???“汽车”,“苹果”,“狗”,“猫”

我想在LINQ中做这个并试过这个;

var newList = from l1 in listOne
              join l2 in listTwo on l1 equals l2 in temp
              from nl temp.DefaultIfEmpty()
              select nl;

但它返回null,显然我的Linq-Fu很弱.任何建议表示赞赏.

解决方法

你需要listOne中listTne的所有项目,然后是listOne中的剩余项目吗?

var results = listTwo.Intersect(listOne).Union(listOne);
foreach (string r in results)
{
    Console.WriteLine(r);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读