为了账号安全,请及时绑定邮箱和手机立即绑定

检查清单的价值

检查清单的价值

C#
慕容708150 2022-06-19 10:15:48
我有一个用作列表的类: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 方法,但我无法让它工作。有人可以帮忙吗?
查看完整描述

4 回答

?
LEATH

TA贡献1936条经验 获得超7个赞

我想你能做的是

var result = defaultOptions.Where(x=>x.method.Contains(yourStringValue).ToList();


查看完整回答
反对 回复 2022-06-19
?
明月笑刀无情

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


查看完整回答
反对 回复 2022-06-19
?
潇潇雨雨

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


查看完整回答
反对 回复 2022-06-19
?
茅侃侃

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

            }


        }


    }

}


查看完整回答
反对 回复 2022-06-19
  • 4 回答
  • 0 关注
  • 177 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号