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 關注
- 176 瀏覽
添加回答
舉報