我想使用这个 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"
},
};
}
}
如果我搜索“DAR”,它会返回两个,但“dar”会返回 0 个项目。 我将如何修改此查询?
最佳答案
假设您正在谈论 x.StationName.Contains(search)
您可以这样做
var list = data
.Where(x => x.StationName.IndexOf(search, StringComparison.OrdinalIgnoreCase) >= 0)
您还可以添加一个String
扩展方法
public static class StringExtensions
{
public static bool Contains(this string thisObj, string value, StringComparer compareType)
{
return thisObj.IndexOf(value, StringComparison.OrdinalIgnoreCase) >= 0;
}
}
然后像这样使用它
var list = data
.Where(x => x.StationName.Contains(search, StringComparison.OrdinalIgnoreCase));
关于C# Lambda 忽略大小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31077403/