1

比如说我有一个如下列表:

List<string> myList = new List<string>();

其中包含:

item1
item 2
testing 123

我怎么能说:

"where list item contains the value "123" return that item in full"

因此输出结果将是:

testing 123
4

5 回答 5

3

Try this:

List<string> myList = new List<string>()
{
    "item1",
    "item 2",
    "testing 123",
};

var contains123 = myList.Where(x => x.Contains("123"));
于 2012-08-14T10:44:23.677 回答
3
string s = myList.Where(p => p.Contains("123")).SingleOrDefault();

or

string s = myList.SingleOrDefault(p => p.Contains("123"));
于 2012-08-14T10:45:06.633 回答
2

看一看

        var list = new List<string>
            {
                "item1",
                "item 2",
                "testing 123"
            };
        var result = list.Find(x => x.Contains("123"));
于 2012-08-14T10:45:59.520 回答
1
string result = myList.Select(item => item.Contains("123")).FirstOrDefault();

if (result != null)
  Console.WriteLine(result);
于 2012-08-14T10:43:56.810 回答
0

you can do that by using LINQ.

Example:

myList.Single(x => x == "testing 123");

if that is what you are looking for.

于 2012-08-14T10:43:17.730 回答