C# 使用Linq比较两个列表的部分匹配

C# 使用Linq比较两个列表的部分匹配,c#,linq,contains,C#,Linq,Contains,我试着浏览了其他一些问题,但找不到任何部分匹配的问题 我有两个列表 它们里面有密码。一个是所选代码的列表,一个是所需代码的列表。但是,整个代码列表是一棵树,因此它们有子代码。例如 代码B 代码B.1 代码B.11 假设所需代码为B,但其树下的任何内容都将满足该要求,因此,如果所选代码为A和C,则匹配将失败,但如果所选代码之一为B.1,则包含部分匹配 我只需要知道所选的代码是否与所需的代码部分匹配。这是我目前的尝试 //Required is List<string> and Sele

我试着浏览了其他一些问题,但找不到任何部分匹配的问题

我有两个
列表

它们里面有密码。一个是所选代码的列表,一个是所需代码的列表。但是,整个代码列表是一棵树,因此它们有子代码。例如 代码B 代码B.1 代码B.11

假设所需代码为B,但其树下的任何内容都将满足该要求,因此,如果所选代码为A和C,则匹配将失败,但如果所选代码之一为B.1,则包含部分匹配

我只需要知道所选的代码是否与所需的代码部分匹配。这是我目前的尝试

//Required is List<string> and Selected is a List<string>
int count = (from c in Selected where c.Contains(Required.Any()) select c).Count();
//必填项为列表,选定项为列表
int count=(从c.Contains(必需的.Any())中选择c.count();
我得到的错误在Required.Any()上,它不能从bool转换为string


抱歉,如果这让人困惑,请告诉我添加任何其他信息是否有帮助。

我想您需要这样的信息:

using System;
using System.Collections.Generic;
using System.Linq;

static class Program {
    static void Main(string[] args) {
        List<string> selected = new List<string> { "A", "B", "B.1", "B.11", "C" };
        List<string> required = new List<string> { "B", "C" };
        var matching = from s in selected where required.Any(r => s.StartsWith(r)) select s;
        foreach (string m in matching) {
            Console.WriteLine(m);
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
静态类程序{
静态void Main(字符串[]参数){
选定名单=新名单{“A”、“B”、“B.1”、“B.11”、“C”};
所需列表=新列表{“B”,“C”};
var matching=在需要的地方从s中选择。任意(r=>s.StartsWith(r))选择s;
foreach(匹配中的字符串m){
控制台写入线(m);
}
}
}

以这种方式在
必需的
上应用
任何
条件都会给您提供匹配的元素-我不确定您是否应该使用
StartsWith
Contains
,这取决于您的需求。

如果选择的和必需的列表足够大,则以下比公认的答案快:

static void Main(string[] args)
{
    List<string> selected = new List<string> { "A", "B", "B.1", "B.11", "C" };
    List<string> required = new List<string> { "B", "C" };
    required.Sort();
    var matching = selected.Where(s =>
    {
        int index = required.BinarySearch(s);
        if (index >= 0) return true; //exact match
        index = ~index;
        if (index == 0) return false;
        return s.StartsWith(required[index - 1]);
    });
    foreach (string m in matching)
    {
        Console.WriteLine(m);
    }
}
static void Main(字符串[]args)
{
选定名单=新名单{“A”、“B”、“B.1”、“B.11”、“C”};
所需列表=新列表{“B”,“C”};
required.Sort();
变量匹配=选中。其中(s=>
{
int index=必需。二进制搜索;
如果(索引>=0)返回true;//精确匹配
索引=~index;
如果(索引==0)返回false;
返回s.StartsWith(必需[索引-1]);
});
foreach(匹配中的字符串m)
{
控制台写入线(m);
}
}

给定
n=required.Count
m=required.Count
接受的答案算法复杂度为
O(n*m)
。然而,我提出的算法复杂度更好:
O((n+m)*Log(n))

此查询查找两个列表中存在的任何匹配项。如果两个列表中都存在一个值,则返回
true
,否则返回
false

List<string> listString1 = new List<string>();
List<string> listString2 = new List<string>();

listString1.Add("A");
listString1.Add("B");
listString1.Add("C");
listString1.Add("D");
listString1.Add("E");

listString2.Add("C");
listString2.Add("X");
listString2.Add("Y");
listString2.Add("Z");

bool isItemExist = listString1.Any(x => listString2.Contains(x));
List listString1=new List();
List listString2=新列表();
清单1.添加(“A”);
清单1.添加(“B”);
清单1.添加(“C”);
清单1.添加(“D”);
清单1.添加(“E”);
清单2.添加(“C”);
列表2.添加(“X”);
清单2.添加(“Y”);
列表2.添加(“Z”);
bool isItemExist=listString1.Any(x=>listString2.Contains(x));

效果很好,谢谢您的帮助。今天我已经在这里呆了这么久了,我的大脑变得一团糟,我只是一直盯着这个东西想弄明白。非常好的一个,+1!我不知道BinarySearch在没有匹配项的情况下会返回什么(),谢谢。是的!。不幸的是,SortedDictionary没有提供类似的功能。