0

I've got a class which is used as a list:

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

This list gets populated with a stack of values and once finished I'd like to search the Method 'column' to ascertain whether it contains a particular string.

I've searched online and it seems to suggest using the Contains method but I just can't get this to work.

Could anyone assist please?

Thanks, C

SxChoc
  • 619
  • 3
  • 10
  • 26

4 Answers4

1

I think what you can do is

var result = defaultOptions.Where(x=>x.method.Contains(yourStringValue).ToList();
GoldenAge
  • 2,918
  • 5
  • 25
  • 63
0

You can below approach:

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

        }

    }
}
Serhat Oz
  • 788
  • 8
  • 12
0

An option could be to create an extension method for List<T> in your program. This way, you can quickly and easily use the method every time you use 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;
    }

Using ints, here is an example of this method in action:

    List<int> ints = new List<int> { 0, 2, 1, 3, 4 };
    Console.WriteLine(ints.GetIndex(2));
MAO3J1m0Op
  • 423
  • 3
  • 14
0

There are many ways to do this:

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

Here is a writeup on Contains vs Any: What is the difference between Contains and Any in LINQ?

Gauravsa
  • 6,330
  • 2
  • 21
  • 30