LINQ的名单排序基于另一个列表

编程入门 行业动态 更新时间:2024-10-23 13:36:22
本文介绍了LINQ的名单排序基于另一个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有两个通用的列表的对象,其中之一含有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的名单排序基于另一个列表

本文发布于:2023-11-30 00:50:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648174.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:名单   列表   LINQ

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!