如何使用LINQ Contains(string [])而不是Contains(string)

Spo*_*com 94 c# linq string contains

我有一个大问题.

我有一个linq查询,它只是看起来像这样:

from xx in table
where xx.uid.ToString().Contains(string[])
select xx
Run Code Online (Sandbox Code Playgroud)

string[]数组的值将是(1,45,20,10等...)

为默认.Contains.Contains(string).

我需要它来代替: .Contains(string[])...

编辑:一位用户建议为其编写扩展类string[].我想知道如何,但任何人都愿意指出我正确的方向?

编辑: uid也是一个数字.这就是它被转换为字符串的原因.

帮助任何人?

tva*_*son 79

spoulson有它接近直角的,但你需要创建一个List<string>string[]第一.实际上List<int>如果uid也会更好int. List<T>支持Contains().这样做 uid.ToString().Contains(string[])会暗示作为字符串的uid包含数组的所有值作为子字符串??? 即使你确实写了扩展方法,它的意义也是错误的.

[编辑]

除非你改变它并将其写string[]为Mitch Wheat演示,否则你只能跳过转换步骤.

[EndEdit中]

这是你想要的,如果你不做扩展方法(除非你已经有潜在的uid集合作为整数 - 然后只是使用List<int>()).这使用了链式方法语法,我认为它更清晰,并且转换为int以确保查询可以与更多提供程序一起使用.

var uids = arrayofuids.Select(id => int.Parse(id)).ToList();

var selected = table.Where(t => uids.Contains(t.uid));
Run Code Online (Sandbox Code Playgroud)

  • 根据MSDN,string []实现了IEnumerable <T>,它有一个Contains方法.因此,没有必要将数组转换为IList <T>.http://msdn.microsoft.com/en-us/library/19e6zeyy.aspx (3认同)

Jas*_*son 33

如果您真的想复制Contains,但是对于数组,这里有一个扩展方法和示例代码用于:

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

namespace ContainsAnyThingy
{
    class Program
    {
        static void Main(string[] args)
        {
            string testValue = "123345789";

            //will print true
            Console.WriteLine(testValue.ContainsAny("123", "987", "554")); 

            //but so will this also print true
            Console.WriteLine(testValue.ContainsAny("1", "987", "554"));
            Console.ReadKey();

        }
    }

    public static class StringExtensions
    {
        public static bool ContainsAny(this string str, params string[] values)
        {
            if (!string.IsNullOrEmpty(str) || values.Length > 0)
            {
                foreach (string value in values)
                {
                    if(str.Contains(value))
                        return true;
                }
            }

            return false;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 我想你的意思是!string.IsNullOrEmpty(str)&& values.Length> 0 (4认同)
  • +1 @Jason,你应该完全提交给http://ExtensionMethod.net感谢优秀的代码,它解决了我今天的问题! (2认同)

Jar*_*Par 18

请尝试以下方法.

string input = "someString";
string[] toSearchFor = GetSearchStrings();
var containsAll = toSearchFor.All(x => input.Contains(x));
Run Code Online (Sandbox Code Playgroud)

  • 我根本不知道这是怎么回答这个问题的.这个问题是否适合你? (5认同)
  • 我真的希望人们在标记你的时候发表评论.特别是因为我提供的答案100%正确. (2认同)

RJ *_*han 15

.NET 4.0中的LINQ为您提供了另一种选择; .Any()方法;

string[] values = new[] { "1", "2", "3" };
string data = "some string 1";
bool containsAny = values.Any(data.Contains);
Run Code Online (Sandbox Code Playgroud)


Jum*_*zza 6

或者如果您已经在列表中有数据并且更喜欢其他Linq格式:)

List<string> uids = new List<string>(){"1", "45", "20", "10"};
List<user> table = GetDataFromSomewhere();

List<user> newTable = table.Where(xx => uids.Contains(xx.uid)).ToList();
Run Code Online (Sandbox Code Playgroud)