简体   繁体   English

使用外部iEnumerable对iEnumerable进行排序

[英]Sorting iEnumerable with external iEnumerable

I've been trying to sort a iEnumerable using another iEnumerable as a point of reference. 我一直在尝试使用另一个iEnumerable作为参考点对一个iEnumerable进行排序。

My first iEnumerable "combinations" (the one which i'd like to sort) holds 67 items but the only important property of these items is InventSizeName. 我的第一个iEnumerable“组合”(我想排序)包含67个项目,但是这些项目的唯一重要属性是InventSizeName。

My 2nd iEnumerable "sizes" holds 5 items and the items looks like this 我的第二个iEnumerable“大小”包含5个项目,这些项目看起来像这样

Id Name SortOrder ID名称SortOrder

What I would like to do is to sort combinations by using sizes.SortOrder where sizes.Name == combinations.InventSizeName. 我想做的是使用sizes.SortOrder对组合进行排序,其中size.Name == groups.InventSizeName。

The closest I've been is 我最近的是

        var sorted = combinations
        .Zip(sizes, (c, s) => new { com = c, siz = s })
        .OrderBy(v => v.siz.Order)
        .Select(v => v.com)
        .ToList();

This however doesn't compare the properties and set the correct order (obviously) and it gives me a List with only 5 entries. 但是,这并没有比较属性并设置正确的顺序(显然),它给了我一个只有5个条目的列表。

I'm sorry that this question is badly written but I hope anyone of you guys could help me out here. 对不起,这个问题写得不好,但我希望你们中的任何人都可以在这里帮助我。

I think the easiest way is to join the two IEnumerables 我认为最简单的方法是加入两个IEnumerables

var sorted = from c in combinations
             join s in sizes on c.Name equals s.InventSizeName
             orderby s.Order
             select c;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM