搜索嵌套列表的最快方法<>在 C# 中

发布于 2024-11-19 08:23:04 字数 527 浏览 1 评论 0 原文

我有一个列表<>其中包含另一个 List<>

我需要查找给定值是否存在于最里面列表中的任何项目中。 如果找到匹配项,我需要该特定项目并返回。

我正在这样做,如下所示:

InnerList inner = null;
foreach(TopList in topListItems)
{
    inner = asn.Owners.Find(x => x.GuestId == guestId);
    if(inner != null)
         break;
}

//item found if inner is not null
//else item absent in the inner list

Any other alternate way that may run faster than this?

编辑: 一些更正:我只需要查看内部列表是否有具有特定值的项目。 如果是,那么我需要返回具有匹配项的顶级项目。 我想逻辑是一样的。

I have a List<> which contains another List<>

I need to find if a given value is present in any of the items in the innermost list.
If match found, I need that specific item and return.

I am doing this as shown below:

InnerList inner = null;
foreach(TopList in topListItems)
{
    inner = asn.Owners.Find(x => x.GuestId == guestId);
    if(inner != null)
         break;
}

//item found if inner is not null
//else item absent in the inner list

Any other alternate way that may run faster than this?

EDIT:
Some correction: I just need to see if inner list has an item with a specific value.
If yes, then I need to return the top level item that that has the match.
I guess the logic is the same.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

坦然微笑 2024-11-26 08:23:04

这就是我使用 Linq 实现这一目标的方式。

var answer = from topList in TopListItems
             from innerListItem in topList.InnerList
             where innerListItem.GuestId == guestId
             select topList;

或按照 Claytons 评论使用 Lambdas

var answer = TopListItems.FirstOrDefault(topList => 
             topList.InnerList.Any(innerList => 
             innerList.GuestId == guestId));

但是,使用 GuestId 进行重构以使用键控字典会更快。

This would be how I would achieve this using Linq.

var answer = from topList in TopListItems
             from innerListItem in topList.InnerList
             where innerListItem.GuestId == guestId
             select topList;

or using Lambdas as per Claytons comment

var answer = TopListItems.FirstOrDefault(topList => 
             topList.InnerList.Any(innerList => 
             innerList.GuestId == guestId));

However refactoring to use a keyed dictionary using GuestId would be faster.

站稳脚跟 2024-11-26 08:23:04

如果您想保留数据结构,那么我看到的唯一改进就是放弃基于委托的搜索和手动搜索。我预计这会带来约二倍的改善。

foreach(var innerList in outerList)
{
    foreach(var item in innerList)
    {
        if(item.GuestId == guestId)
            return innerList;
    }
}
return null;

如果可能的话,您可以以某种方式使用字典。但我对你的问题了解不够,无法告诉你这是否可能。这可以带来非常大的加速,因为在字典中按键搜索是 O(1) 而不仅仅是 O(n)。

在某些情况下,for 循环可能会比 foreach 循环稍微加速,但我不知道这是否是其中之一。所以你需要进行基准测试。

If you want to keep the data structure then the only improvement I see is throwing out the delegate based search and search manually. I expect an improvement of about factor two with that.

foreach(var innerList in outerList)
{
    foreach(var item in innerList)
    {
        if(item.GuestId == guestId)
            return innerList;
    }
}
return null;

If possible you could employ dictionaries in some way. But I don't know enough about your problem to tell you if that's possible. This can give a really big speedup since a search by key in a dictionary is O(1) and not just O(n).

In some situations a for loop might give a slight speedup over the foreach loop, but I don't know if this is one of them. So you'll need to benchmark.

何处潇湘 2024-11-26 08:23:04

您可以递归地执行此操作。这段代码可能不适合你,但它会是这样的:

public object loopList(List dList,object searchvalue)
{
    foreach(dList中的对象值)
    {
      if(搜索值 == 值)
      {
        返回值;
      }
      别的
      {
         循环列表((列表<对象>)值);
       }
    }
}

You can do it recursively. This code probably won't work for you, but it's gonna be something like it:

public object loopList(List<object> dList,object searchvalue)
{
    foreach(object value in dList)
    {
      if(searchvalue == value)
      {
        return value;
      }
      else
      {
         loopList((List<object>)value);
       }
    }
}
薆情海 2024-11-26 08:23:04

内部列表是否已排序?如果是这样,您可以对内部列表使用二分搜索,这将提供潜在的性能改进。此外,如果您可以控制内部列表的构造,则将其更改为以 GuestId 为键的字典将为您提供更优化的检查。

Is the inner list sorted? If so you could use a binary search for the inner list which would provide a potential performance improvement. Additionally if you have control over the construct of the inner list, changing it to a Dictionary keyed on GuestId would give you a more optimal check.

趴在窗边数星星i 2024-11-26 08:23:04

可能是这个吗?

InnerList inner = null;
foreach (var innr in outerList.SelectMany(c =>c.Owners
                              .Where(x => x.GuestId == guestId)))
{
    inner = innr;
}

Could be this?

InnerList inner = null;
foreach (var innr in outerList.SelectMany(c =>c.Owners
                              .Where(x => x.GuestId == guestId)))
{
    inner = innr;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文