简体   繁体   English

C#清单 <string> 包含来自另一个列表的部分匹配 <string>

[英]C# List<string> contains partial match from another List<string>

I have these lists: 我有这些清单:

var list1 = new List<string>
{
    "BOM_Add",
    "BOM_Edit",
    "BOM_Delete",
    "Paper_Add",
    "Paper_Edit",
    "Paper_Delete"
};

var list2 = new List<string> {"BOM", "Paper_Add"};

I want to create a third list of the common items based on a partial match. 我想基于部分匹配来创建常见项目的第三个列表。 So, the third list should contain: 因此,第三个列表应包含:

"BOM_Add",
"BOM_Edit",
"BOM_Delete",
"Paper_Add"

because the second list contains "BOM". 因为第二个列表包含“ BOM”。

If the second list contained "_Edit", then I would expect the third list to have 如果第二个列表包含“ _Edit”,那么我希望第三个列表具有

"BOM_Edit",
"Paper_Edit"

I know how to do this with .Intersect() if I spell out each item (eg "BOM_Add") in the second list, but I need it to be more flexible than that. 如果我拼出第二个列表中的每个项目(例如“ BOM_Add”),我都知道如何使用.Intersect()来实现,但是我需要比它更灵活。

Can this be done without iterating through each item on the first list? 可以在不迭代第一个列表中的每个项目的情况下完成此操作吗? These lists may get very long and I would prefer to avoid that if I can. 这些列表可能会很长,如果可以的话,我宁愿避免这样做。

You can use LINQ 您可以使用LINQ

var result = list1.Where(r => list2.Any(t => r.Contains(t)))
                  .ToList();

For output: 对于输出:

foreach (var item in result)
{
    Console.WriteLine(item);
}

Output would be: 输出为:

BOM_Add
BOM_Edit
BOM_Delete
Paper_Add

Can this be done without iterating through each item on the first list? 可以在不迭代第一个列表中的每个项目的情况下完成此操作吗?

You have to iterate, either through a loop or using LINQ (which internally iterates as well) 您必须通过循环或使用LINQ (也在内部进行迭代)进行迭代

Can this be done without iterating through each item on the first list? 可以在不迭代第一个列表中的每个项目的情况下完成此操作吗?

No, if you want to find all of the items that contain one of the items that you have. 否,如果要查找包含您拥有的物品之一的所有物品。 There is no way of building an index, or any sort of structure that can rule out large sections of items without checking each one. 没有建立索引的方法,也没有任何一种结构可以排除大部分项目而不检查每个项目的结构。 The only option is to compare every single item in the first list with every single item in the other list, doing your Contains check. 唯一的选择是比较第一个列表中的每个项目与另一个列表中的每个项目,并进行“ Contains检查。

If you only needed to do a StartsWith instead of a Contains , then you could sort your list, do a BinarySearch to find the item nearest to the item that you're searching for, which would allow you to easily find all of the items that start with a particular string while only actually needing to check O(log(n) + m) items (where n is the size of the list an m is the average number of matches). 如果您只需要执行StartsWith而不是Contains ,则可以对列表进行排序,请执行BinarySearch来查找与您要搜索的项目最近的项目,这将使您可以轻松地找到所有从一个特定的字符串开始,而实际上只需要检查O(log(n)+ m)个项(其中n是列表的大小, m是平均匹配数)。 You could do the same thing with an EndsWith too, if you just sorted items based on the reverse of the string, but there's no way to sort an items such that a Contains check does this. 如果您只是根据字符串的相反方向对项目进行排序,那么也可以对EndsWith进行相同的操作,但是无法对项目进行排序,例如, Contains检查可以做到这一点。

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

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