比如说我有一个如下列表:
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
Try this:
List<string> myList = new List<string>()
{
"item1",
"item 2",
"testing 123",
};
var contains123 = myList.Where(x => x.Contains("123"));
string s = myList.Where(p => p.Contains("123")).SingleOrDefault();
or
string s = myList.SingleOrDefault(p => p.Contains("123"));
看一看
var list = new List<string>
{
"item1",
"item 2",
"testing 123"
};
var result = list.Find(x => x.Contains("123"));
string result = myList.Select(item => item.Contains("123")).FirstOrDefault();
if (result != null)
Console.WriteLine(result);
you can do that by using LINQ.
Example:
myList.Single(x => x == "testing 123");
if that is what you are looking for.