亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

檢查清單的價值

檢查清單的價值

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 關注
  • 176 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號