基于其他列表的列表排序

基于其他列表的列表排序

本文介绍了基于其他列表的列表排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个通用列表对象,其中一个包含ID和顺序,另一个则包含一堆ID,例如,第二个列表中的每个ID都具有对第一个列表的ID引用;

I have two generic list objects, in which one contains ids and ordering, and the other a bunch of ids with each id in the second list having an id reference to the first list, for example;

public class OptionType
{
    public int ID { get; set; }
    public int Ordering { get; set; }
}

public class Option
{
    public int ID { get; set; }
    public int Type_ID { get; set; }
}

很明显,我可以通过对OptionType列表进行简单的排序

Obviously I can do a simple sort on a list of OptionTypes by doing

types_list.OrderBy(x => x.Ordering);

但是,问题是,我该如何利用对象上的"type_ID"来排序"options_list",这与types_list的排序有关.就像这样(显然这是无效的-但希望您会明白这一点!)

Question is though, how could I go about ordering an 'options_list' by utilising the 'Type_ID' on the object which would relate to the ordering of the types_list. As in something like (obviously this isn't valid - but hopefully you will get the idea!)

options_list.OrderBy(x => x.Type_ID == types_list.OrderBy(e => e.Ordering));

推荐答案

您应该能够使用联接来产生所需的输出.使用查询语法的示例.

You should be able to use a join to produce your desired output. Example using query syntax.

var orderedOptions = from option in options_list
                     join type in types_list
                     on option.Type_ID equals type.ID
                     orderby type.Ordering
                     select option;

这篇关于基于其他列表的列表排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 06:41