基于另一个列表的列表排序 [英] List sort based on another list

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

问题描述

我有两个通用列表对象,其中一个包含 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; }
}   

显然,我可以通过执行以下操作对 OptionTypes 列表进行简单排序

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

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

问题是,我如何通过使用与 types_list 的排序相关的对象上的Type_ID"来订购options_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;

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

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