使用LINQ搜索两个匹配列表,并为每个项返回布尔值

时间:2018-02-27 22:00:37

标签: c# linq

我正在尝试检查两个列表,例如看起来像这样

list1 = {"br","je"}; 
list2 = {"banana", "bread", "jam", "brisket", "flakes", "jelly"};

应该返回

{false, true, false, true, false, true}

使用LINQ可以实现上述输出。我尝试过像这样的东西

    public static IEnumerable<bool> ContainsValues(List<string> list1, List<string> list2)
{
    List<bool> res = new List<bool>();

    foreach (string item in list1)
    {
        foreach (string sub in list2)
        {
            res.Add(item.ToLower().Contains(sub.ToLower()));
        }
    }

    return res;
}

4 个答案:

答案 0 :(得分:8)

是的,您可以在一个声明中重写现有代码:

public static IEnumerable<bool> ContainsValues(List<string> list1, List<string> list2)
{
    return
        from item in list1
        from sub in list2
        select item.ToLower().Contains(sub.ToLower());
}

但是,您现有的代码不提供您想要的示例结果。为了得到你想要的结果,我想你想要这样的东西:

public static IEnumerable<bool> SecondContainsFirst(List<string> first, 
    List<string> second)
{
    return second.Select(s => first.Any(f => 
        s.IndexOf(f, StringComparison.OrdinalIgnoreCase)) > -1);
}

此处的示例用法:

private static void Main()
{
    var list1 = new List<string> {"br", "je"};
    var list2 = new List<string> {"banana", "bread", "jam", "brisket", "flakes", "jelly"};
    var result = SecondContainsFirst(list1, list2);

    Console.WriteLine($"{{{string.Join(", ", result)}}}");

    Console.Write("\nDone!\nPress any key to exit...");
    Console.ReadKey();
}

<强>输出

enter image description here

答案 1 :(得分:4)

IEnumerable<bool> res = list2.Select(q => list1.Any(q.Contains));

答案 2 :(得分:2)

后来和当时koryakinp's answer相同 - 花了我一些时间让它成为一个完整的例子 - 他不是。

list2.Select(item => list1.Any(l1 => item.IndexOf(l1,StringComparison.OrdinalIgnoreCase) >= 0));将产生所需的输出:

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

public class Program
{
    public static void Main(string[] args)
    {
        var list1 = new List<string> { "br", "je" };
        var list2 = new List<string> { "banana", "bread", "jam", 
                                       "brisket", "flakes", "jelly" };

        var res = list2.Select(item => 
            list1.Any(l1 => item.IndexOf(l1, StringComparison.OrdinalIgnoreCase) >= 0));


        Console.WriteLine(string.Join(",", list1));
        Console.WriteLine(string.Join(",", list2));
        Console.WriteLine(string.Join(",", res));

        Console.ReadLine();
    }
}

输出:

br,je
banana, bread, jam, brisket, flakes, jelly
False,True,False,True,False,True  // no idea why yours are lower case

建议编辑:

  

要通过使用除序数比较之外的其他内容(例如区分文化比较或序数不区分大小写的比较)来确定字符串是否包含指定的子字符串,您可以创建自定义方法。以下示例说明了一种此类方法。

后面是使用IndexOf() +所需StringComparison的示例。

答案 3 :(得分:1)

如果只是因为使用了所有3个当前答案,恕我直言,麻烦的字符串操作:

 public static void Main(string[] args)
{
    var list1 = new List<string> { "br", "je" };
    var list2 = new List<string> { "banana", "bread", "jam", "brisket", "flakes", "jelly" };

    var result = string.Join(",",ContainsValues(list1, list2));

    Console.WriteLine($"{string.Join(",",list1)}");
    Console.WriteLine($"{string.Join(",", list2)}");
    Console.WriteLine($"{result}");
    Console.Read();

    IEnumerable<bool> ContainsValues(List<string> lst1, List<string> lst2) =>
      lst2.Select(l2 => lst1.Any(l1 => l2.IndexOf(l1, StringComparison.OrdinalIgnoreCase) >= 0));
}   

`

`