2

I want to ignore case using this LAMBDA query:

public IEnumerable<StationDto> StationSearch(string search)
        {
            var data = GenerateDtos();

            var list = data.Where(x => x.StationName.Contains(search));




            //var searchDto = new SearchDto {



            return null;
        }



        private static IEnumerable<StationDto> GenerateDtos()
        {
            return new List<StationDto>()
            {
                new StationDto()
                {
                    StationId = 1,
                    StationName = "DARTFORD"
                },
                new StationDto()
                {
                    StationId = 2,
                    StationName = "DARTMOUTH"
                },
                new StationDto()
                {
                    StationId = 3,
                    StationName = "TOWER HILL"
                },
                new StationDto()
                {
                    StationId = 4,
                    StationName = "DERBY"
                },
                new StationDto()
                {
                    StationId = 5,
                    StationName = "lIVERPOOL"
                },
                new StationDto()
                {
                    StationId = 6,
                    StationName = "LIVERPOOL LIME STREET"
                },
                new StationDto()
                {
                    StationId = 7,
                    StationName = "PADDINGTON"
                },
                new StationDto()
                {
                    StationId = 8,
                    StationName = "EUSTON"
                },
                new StationDto()
                {
                    StationId = 9,
                    StationName = "VICTORIA"
                },
            };
        }
    }

If I search for "DAR" it will bring back two but "dar" brings back 0 items. How would I modify this query?

nick gowdy
  • 6,191
  • 25
  • 88
  • 157

5 Answers5

5

Assuming you're talking about x.StationName.Contains(search) you could do

var list = data
  .Where(x => x.StationName.IndexOf(search, StringComparison.OrdinalIgnoreCase) >= 0)

You could also add a String extension method

public static class StringExtensions
{
    public static bool Contains(this string thisObj, string value, StringComparer compareType) 
    {
        return thisObj.IndexOf(value, StringComparison.OrdinalIgnoreCase) >= 0;
    }
}

and use it like this

 var list = data
   .Where(x => x.StationName.Contains(search, StringComparison.OrdinalIgnoreCase));
Claudio Redi
  • 67,454
  • 15
  • 130
  • 155
4

Something like this would work:

var list = data.Where(x => x.StationName.ToLower().Contains(search.ToLower()));

Or

var list = data.Where(x => x.StationName.Contains(search, StringComparer.OrdinalIgnoreCase);

Not sure the syntax is correct on the second. I think it is.

Randy Minder
  • 47,200
  • 49
  • 204
  • 358
  • Both of these (especially the first one) are *far* more readable than `IndexOf(...) >= 0`. Good answer. – kdbanman Jun 26 '15 at 17:05
1

Use something like below:

var list = data.Where(x => x.StationName.ToUpper().Contains(search.ToUpper()));
ATP
  • 553
  • 1
  • 3
  • 16
1

You could utilize Fody which is a IL Weaving tool created for .net and use the Caseless addon.

When utilizing this library you can write code like this (as per the documentation):

public bool Foo()
{
    var x = "a";
    var y = "A";
    return x == y;
}

but would be compiled as:

public bool Foo()
{
    var x = "a";
    var y = "A";
    return string.Equals(x, y, StringComparison.OrdinalIgnoreCase);
}

So that you don't have to remember to either set ignorecase or ToUpper/ToLower. I don't know about you, but I can't think of a time when I actually wanted case sensitive compares of strings... this could be applied to the entire project so you never have to remember to do it again! :)

In your specific example, the existing code you're using of:

var list = data.Where(x => x.StationName.Contains(search));

would bring back all instances of "DAR" indiscriminate of case.

Community
  • 1
  • 1
Kritner
  • 13,557
  • 10
  • 46
  • 72
0

This worked the best for me. I was needing to do contains on a List.

Extension Method:

public static bool In(this List<string> thisObj, string value)
{
    foreach (var v in thisObj)
    {
        if (v.Equals(value, StringComparison.OrdinalIgnoreCase))
            return true;
    };
    return false;
}

Usage:

public bool IsFound(string value){
    return someStringList.In(value);
}

I went with "In" to distinguish from the Contains method.

BondAddict
  • 599
  • 5
  • 12