C#Lambda忽略大小写

nic*_*wdy 2 c# lambda

我想忽略使用此LAMBDA查询的情况:

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"
                },
            };
        }
    }
Run Code Online (Sandbox Code Playgroud)

如果我搜索"DAR"它会带回两个,但"dar"会带回0个项目.我该如何修改此查询?

Cla*_*edi 5

假设你在谈论x.StationName.Contains(search)你可以做到的

var list = data
  .Where(x => x.StationName.IndexOf(search, StringComparison.OrdinalIgnoreCase) >= 0)
Run Code Online (Sandbox Code Playgroud)

您还可以添加String扩展方法

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

并像这样使用它

 var list = data
   .Where(x => x.StationName.Contains(search, StringComparison.OrdinalIgnoreCase));
Run Code Online (Sandbox Code Playgroud)