4 回答

TA贡献1936条经验 获得超7个赞
我想你能做的是
var result = defaultOptions.Where(x=>x.method.Contains(yourStringValue).ToList();

TA贡献1828条经验 获得超4个赞
一个选项可能是List<T>在您的程序中创建一个扩展方法。这样,您每次使用时都可以快速轻松地使用该方法List<T>。
/// <summary>
/// Gets the index of a given <paramref name="component"/> of the <see cref="List{T}"/>.
/// </summary>
/// <returns>The index of a given <paramref name="component"/> of the <see cref="List{T}"/>.</returns>
/// <param name="value">The <see cref="List{T}"/> to find the <paramref name="component"/> in.</param>
/// <param name="component">The component to find.</param>
/// <typeparam name="T">The type of elements in the list.</typeparam>
public static int? GetIndex<T> (this List<T> value, T component)
{
// Checks each index of value for component.
for (int i = 0; i < value.ToArray().Length; ++i)
if (value[i].Equals(component)) return i;
// Returns null if there is no match
return null;
}
使用整数,这里是这个方法的一个例子:
List<int> ints = new List<int> { 0, 2, 1, 3, 4 };
Console.WriteLine(ints.GetIndex(2));

TA贡献1833条经验 获得超4个赞
有很多方法可以做到这一点:
string stringToSearch = "someString";
if (defaultOptions.Select(t => t.method).Contains(stringToSearch)) { ... }
or, if you prefer to use Any(), then can use this:
if (defaultOptions.Any(t => t.method == stringToSearch)) { ... }
// if you'd like to return first matching item, then:
var match = defaultOptions
.FirstOrDefault(x => x.Contains(stringToSearch));
if(match != null)
//Do stuff

TA贡献1842条经验 获得超22个赞
您可以采用以下方法:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Rextester
{
public class StudyOptions {
public decimal price { get; set; }
public string currency { get; set; }
public string currencyIdentifier { get; set; }
public bool lowGDP { get; set; }
public string method { get; set; }
}
public class Program
{
public static void Main(string[] args)
{
List<StudyOptions> defaultOptions = new List<StudyOptions>();
defaultOptions.Add(new StudyOptions{ price = 0, currency = "t", currencyIdentifier = ".", lowGDP = false, method = "method"});
foreach(var studyOptions in defaultOptions){
if(studyOptions.method.Contains("method") )
Console.WriteLine(studyOptions);
}
}
}
}
- 4 回答
- 0 关注
- 177 浏览
添加回答
举报