내가 생성 네 가지 방법을 제대로 작동하도록 만드는 일을 기반으로 필터에 여러 매개변수:
bool FilterAreas(AreaPlaceCoordinate apc)
{
if (!AreaHash.Any())
{
return true;
}
foreach (var _ in AreaHash.Where(h => apc.AreaName.Contains(h, StringComparison.OrdinalIgnoreCase)).Select(h => new { }))
{
return true;
}
return false;
}
bool FilterPlaces(AreaPlaceCoordinate apc)
{
if (!PlaceHash.Any())
{
return true;
}
foreach (var _ in PlaceHash.Where(h => apc.PlaceName.Contains(h, StringComparison.OrdinalIgnoreCase)).Select(h => new { }))
{
return true;
}
return false;
}
bool FilterCoordinates(AreaPlaceCoordinate apc)
{
if (!CoordinateHash.Any())
{
return true;
}
foreach (var _ in CoordinateHash.Where(h => apc.CoordinateName.Contains(h, StringComparison.OrdinalIgnoreCase)).Select(h => new { }))
{
return true;
}
return false;
}
bool Filter(AreaPlaceCoordinate apc)
{
return FilterCoordinates(apc)&&FilterPlaces(apc)&& FilterAreas(apc);
}
내 생각으로 할 수 있는 한 방법이지만,그럼에도 불구하고 많은 시도가 나지 않을 처리하는 방법을 알고 있습니다. 그들은 나체에서 사용 예제:
//Hash
private IEnumerable<string> PlaceHash { get; set; } = new HashSet<string>() { };
private IEnumerable<string> AreaHash { get; set; } = new HashSet<string>() { };
private IEnumerable<string> CoordinateHash { get; set; } = new HashSet<string>() { };
public class AreaPlaceCoordinate
{
public int CoordinateId { get; set; }
public string CoordinateName { get; set; }
public int AreaId { get; set; }
public int PlaceId { get; set; }
public string AreaName { get; set; }
public string PlaceName { get; set; }
}
나는 아이디어를 주시면 감사하겠습니다
업데이트
감사,@Abanslash! 또한 추가 텍스트 문자열을 필터링하고 종료와 이와 같은 코드:
private string searchString1 = "";
static bool FilterHash<T>(IEnumerable<string> list, T viewModel, Func<string, bool> p)
{
if (!list.Any())
{
return true;
}
return list.Any(p);
}
public bool TestFilter(AreaPlaceCoordinate apc)
{
bool textEmpty = (string.IsNullOrWhiteSpace(searchString1));
List<string> names = new() { apc.AreaName, apc.PlaceName, apc.CoordinateName };
bool filterText = textEmpty ? true : names.Any(n => n.Contains(searchString1, StringComparison.OrdinalIgnoreCase));
bool areaSelector(string str) => apc.AreaName.Contains(str);
bool coordinateSelector(string str) => apc.CoordinateName.Contains(str);
bool placeSelector(string str) => apc.PlaceName.Contains(str);
return FilterHash(AreaHash, apc, areaSelector) && FilterHash(CoordinateHash, apc, coordinateSelector) && FilterHash(PlaceHash, apc, placeSelector) && filterText;
}