检查清单的价值

我有一个用作列表的类:


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; }

}


List<StudyOptions> defaultOptions = new List<StudyOptions>();

这个列表填充了一堆值,一旦完成,我想搜索方法“列”以确定它是否包含特定的字符串。


我在网上搜索过,似乎建议使用 Contains 方法,但我无法让它工作。


有人可以帮忙吗?


慕容708150
浏览 178回答 4
4回答

LEATH

我想你能做的是var&nbsp;result&nbsp;=&nbsp;defaultOptions.Where(x=>x.method.Contains(yourStringValue).ToList();

明月笑刀无情

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

潇潇雨雨

有很多方法可以做到这一点: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

茅侃侃

您可以采用以下方法:using System;using System.Collections.Generic;using System.Linq;using System.Text.RegularExpressions;namespace Rextester{&nbsp; &nbsp; public class StudyOptions {&nbsp; &nbsp; &nbsp; &nbsp; public decimal price { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string currency { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string currencyIdentifier { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public bool lowGDP { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string method { get; set; }&nbsp; &nbsp; }&nbsp; &nbsp; public class Program&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public static void Main(string[] args)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<StudyOptions> defaultOptions = new List<StudyOptions>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; defaultOptions.Add(new StudyOptions{ price = 0, currency = "t", currencyIdentifier = ".", lowGDP = false, method = "method"});&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach(var studyOptions in defaultOptions){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(studyOptions.method.Contains("method") )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(studyOptions);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP