检查清单的价值

时间:2018-12-19 13:18:53

标签: c# model-view-controller

我有一个用作列表的类:

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方法,但我无法使它正常工作。

有人可以帮忙吗?

谢谢, C

4 个答案:

答案 0 :(得分:1)

我认为您可以做的是

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

答案 1 :(得分:0)

您可以采用以下方式:

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

        }

    }
}

答案 2 :(得分:0)

一个选项可能是在程序中为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));

答案 3 :(得分:0)

有很多方法可以做到这一点:

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

以下是Contains vs Any的内容: What is the difference between Contains and Any in LINQ?