我有两个通用的列表的对象,其中之一含有ID和和订货,并用在具有一个id参考第一列表中的第二列表中的每个ID的其他一堆id的,例如
公共类OptionType{ 公众诠释ID {搞定;组; } 公众诠释订购{搞定;组; }}公共类选项{ 公众诠释ID {搞定;组; } 公众诠释TYPE_ID {搞定;组; }}很明显,我可以通过做做一个简单的排序OptionTypes的名单
types_list.OrderBy(X => x.Ordering);问题是,虽然,我怎么可能去利用这将涉及到TYPES_LIST的顺序对象上的TYPE_ID下令options_list。正如在类似的信息(这显然是无效的 - 但希望你会得到的想法!)
options_list.OrderBy(X => x.Type_ID == types_list.OrderBy(E => e.Ordering));解决方案
您应该能够使用连接来产生所需的输出。使用查询语法的例子。
VAR orderedOptions =从options_list选项 加入TYPES_LIST型 在option.Type_ID等于type.ID 排序依据type.Ordering 选择选项;I have two generic list objects, in which one contains ids and 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; } }Obviously I can do a simple sort on a list of OptionTypes by doing
types_list.OrderBy(x=>x.Ordering);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;
更多推荐
LINQ的名单排序基于另一个列表
发布评论