小编lce*_*rvo的帖子

使用LINQ使用现有数组排序列表

class Car{
     int ID {get;set;}
     string Text {get;set;}
}

List<Car> allCars = new List<Car>(){ new Car{ ID = 1, Text = "one"}, new Car{ ID = 2, Text = "two"}, new Car{ ID = 3, Text = "three"}, new Car{ ID = 4, Text = "four"} };
int[] existingCarIds = new int[]{ 2, 4 };
Run Code Online (Sandbox Code Playgroud)

我想有这个清单.

CarWithID2
CarWithID4
CarWithID1
CarWithID3
Run Code Online (Sandbox Code Playgroud)

所以我做了:

List<Car> carList = allCars.Where(d => existingCarIds.Contains(d.ID)).Concat(allCars.Where(d => !existingCarIds.Contains(d.ID))).ToList();
Run Code Online (Sandbox Code Playgroud)

有没有其他方法可以进行相同的查询?

谢谢

洛伦佐.

c# linq .net-3.5

2
推荐指数
1
解决办法
3205
查看次数

标签 统计

.net-3.5 ×1

c# ×1

linq ×1