c# - 检查字符串是否有任何列表价值观
问题描述
我有一个具有字符串列表作为属性的类:
public class FiltersDto
{
public int? MinPrice { get; set; }
public int? MaxPrice { get; set; }
public int? BathroomCount { get; set; }
public int? BedroomCount { get; set; }
public string Amenities { get; set; }
public List<string> Neighborhoods { get; set; }
}
在这种方法中,我尝试检查“邻居”一词是否在列表中
var sales = await
_propertyRepository
.GetAll()
.Include(x => x.PropertyType)
.WhereIf(input.Neighborhoods != null, x => x.Neighborhood.Contains(???????))
.WhereIf(input.MinPrice.HasValue && input.MaxPrice.HasValue, x => input.MinPrice <= x.Price && x.Price <= input.MaxPrice)
.WhereIf(input.BedroomCount.HasValue, x => x.BedroomCount == input.BedroomCount)
.WhereIf(input.BathroomCount.HasValue, x => x.BathroomCount == input.BathroomCount)
.Where(x => x.PropertyTypeId == 1)
.ToListAsync();
我需要在这一行检查它.WhereIf(input.Neighborhoods != null, x => x.Neighborhood.Contains(?))
我怎么能做到这一点?
解决方案
很难准确说出所描述的内容,但是如果您调用了一个 FiltersDto 的实例myFiltersDto
并且它有一个.Neighborhoods
类似于 is like 的列表{ "Red", "Blue", "Green"}
并且您的 x 的.Neighborhood
字符串是"Blah Red Sand"
x => myFiltersDto.Neighbourhoods.Any(n => x.Neighborhood.Contains(n))
即你问“这些是否Neighborhoods n
出现在字符串中x.Neighborhoods
”
如果您不打算将Contains
其作为字符串内的字符串搜索,并且例如x.Neighborhood
将只是"Red"
并且您正在寻找由它表示的字符串集合中的完全匹配,Neighborhoods
那么它就是
x => myFiltersDto.Neighbourhoods.Contains(x.Neighborhood))
我建议在描述时尽量避免使用“包含”这个词,因为它会在string.Contains
(子字符串搜索)和list.Contains
(元素搜索)之间产生混淆
推荐阅读
- libvlc - 将播放列表文件传递给 LibVLCSharp?
- python - 我想将特定客户的总计分配给变量
- javascript - JS如何根据另一个数组的排序排序()一个数组
- authentication - UniSharp 文件管理器不适用于 Laravel 7
- pandas - 在 DataFrame 上使用 .apply() 时,“Series 的真值不明确”,但如果应用于 Series 则可以
- javascript - 如何在 React 中遍历对象数组
- javascript - 从 javascript 更新输入字段时,Laravel livewire 模型不起作用?
- c - 在二叉搜索树中插入一个节点
- javascript - react-elastic-carousel 动态 itemsToShow 取决于屏幕大小
- google-apps-script - 我的 Google 脚本抛出“意外的字符串错误”