首页 > 解决方案 > 检查字符串是否有任何列表价值观

问题描述

我有一个具有字符串列表作为属性的类:

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

我怎么能做到这一点?

标签: c#.netasp.net-coreaspnetboilerplate

解决方案


很难准确说出所描述的内容,但是如果您调用了一个 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(元素搜索)之间产生混淆


推荐阅读