// 这个表是决定排序顺序的表
List<KeyValuePair<int, string>> sortedList = new List<KeyValuePair<int, string>>();
// 这个表是将要被排序的表,这张表的关联是: 本表的Key=上表的Value
List<KeyValuePair<string, string>> desList = new List<KeyValuePair<string, string>>();
sortedList.Add(new KeyValuePair<int, string>(5, "Item5"));
sortedList.Add(new KeyValuePair<int, string>(10, "Item1"));
sortedList.Add(new KeyValuePair<int, string>(2, "Item2"));
sortedList.Add(new KeyValuePair<int, string>(7, "Item3"));
sortedList.Add(new KeyValuePair<int, string>(4, "Item4"));
//
desList.Add(new KeyValuePair<string, string>("Item1", "Content1"));
desList.Add(new KeyValuePair<string, string>("Item3", "Content3"));
desList.Add(new KeyValuePair<string, string>("Item4", "Content4"));
desList.Add(new KeyValuePair<string, string>("Item2", "Content2"));
desList.Add(new KeyValuePair<string, string>("Item5", "Content5"));
var query = ((from k in sortedList
join p in desList on k.Value equals p.Key
select new { k.Key, p.Value, internalValue = p.Key })
.OrderBy(k => k.Key) // 排序
.Select(n=> new KeyValuePair<string ,string>(n.internalValue,n.Value))).ToList(); //再次生成跟原来类型一样的LIST
for (int i = 0; i < query.Count; i++)
{
Console.WriteLine(string.Format("Key={0},Value={1}", query[i].Key, query[i].Value));
}
Linq排序并声称新对象列表示例
最新推荐文章于 2018-03-26 17:20:38 发布