匹配一个阵列的顺序到另一个使用LINQ [英] Matching the order of one array to another using linq

查看:86
本文介绍了匹配一个阵列的顺序到另一个使用LINQ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的ID的int数组的正确排序。然后,我有一个具有ID属性无序对象的数组。

我想订购由ID匹配int数组的顺序的对象。

沿东西线

  newObjectArray = oldObjectArray.MatchOrderBy(IdArray)

最希望

我觉得我应该能够做到这一点使用LINQ,但我还没有找到一种方式。

我目前的方法似乎并不十分有效,因为它必须对收集的每一次迭代查询。我猜想,性能可能会受到足够大的集合。这最终会发生。

下面是我目前的执行情况:

  //这只是虚拟的数据给你看怎么回事
    INT [] = orderedIDs新INT [5] {5534,5632,2334,6622,2344};
    MemberObject [] =的SearchResult MyMethodToGetSearchResults();    MemberObject [] = orderedSearchResults新MemberObject [orderedIDs.Count()];
    的for(int i = 0; I< orderedIDs.Count();我++)
    {
        orderedSearchResults [I] =的SearchResult
                                                。选择(memberObject => memberObject)
                                                。凡(memberObject => memberObject.id == orderedIDs [I])
                                                .FirstOrDefault();
    }


解决方案

一个蛮力实现:

  MemberObject [] = sortedResults
      IdArray.Select(ID => SearchResult所
                           .FirstOrDefault(项目=> item.id == ID))

但是,这需要重申的SearchResult在IdArray每一个项目,并且不会有重复的ID项目处理太整齐。

如果你让你的搜索结果中ILookup,所以抓住了正确的搜索结果在IdArray每个项目现在Ø情况有所改善(1)时间。

  ILookup< INT,MemberObject> resultLookup = searchResults.ToLookup(X => x.id);

现在

  MemberObject [] = sortedResults
      IdArray.SelectMany(ID => resultLookup [ID])

I have an int array of ID's that are ordered properly. Then I have an an array of unordered objects that have ID properties.

I would like to order the objects by ID that match the order of the int array.

Something along the lines of

newObjectArray = oldObjectArray.MatchOrderBy(IdArray)

would be most desirable

I feel like I should be able to accomplish this using linq but I have yet to find a way.

My current method doesn't seem very efficient since it has to query on every iteration of the collection. I suspect that performance can suffer for sufficiently large collections. Which eventually will happen.

Here is my current implementation:

    //this is just dummy data to show you whats going on
    int[] orderedIDs = new int[5] {5534, 5632, 2334, 6622, 2344};
    MemberObject[] searchResults = MyMethodToGetSearchResults();

    MemberObject[] orderedSearchResults = new MemberObject[orderedIDs.Count()];
    for(int i = 0; i < orderedIDs.Count(); i++)
    {
        orderedSearchResults[i] = searchResults
                                                .Select(memberObject => memberObject)
                                                .Where(memberObject => memberObject.id == orderedIDs[i])
                                                .FirstOrDefault();
    }

解决方案

A brute force implementation:

MemberObject[] sortedResults = 
      IdArray.Select(id => searchResults
                           .FirstOrDefault( item => item.id == id ))

However, this requires reiterating searchResults for every item in IdArray and doesn't deal too neatly with items that have duplicate ids.

Things improve if you make an ILookup of your search results, so that grabbing the correct search result for each item in IdArray is now O(1) time.

ILookup<int, MemberObject> resultLookup = searchResults.ToLookup(x => x.id);

Now:

MemberObject[] sortedResults = 
      IdArray.SelectMany(id => resultLookup[id])

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

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