安
IEnumerable<PropertyTbl>
不是一个
IEnumerable<Address>
Address
PropertyTable
(除非你改变主意
BuildAddressWhereClause
采取行动
IEnumerable<属性tbl>
像这样的事情应该可以做到:
public static IEnumerable<T> BuildAddressWhereClause<T>(IEnumerable<T> source, string value, AddressSearchOptions options, Func<T, Address> addressExtractor) {
string search = value.ToUpper();
if ((options & AddressSearchOptions.Address1To4) == AddressSearchOptions.Address1To4) {
source = source.Where(o => addressExtractor(o).Address1.ToUpper().Contains(search)
|| addressExtractor(o).Address2.ToUpper().Contains(search)
|| addressExtractor(o).Address3.ToUpper().Contains(search)
|| addressExtractor(o).Address4.ToUpper().Contains(search));
}
if ((options & AddressSearchOptions.City) == AddressSearchOptions.City) {
source = source.Where(o => addressExtractor(o).City.ToUpper().Contains(search));
}
if ((options & AddressSearchOptions.PostCode) == AddressSearchOptions.PostCode) {
source = source.Where(o => addressExtractor(o).PostCode.ToUpper().Contains(search));
}
return source;
}
然后你像这样调用它
IEnumerable<PropertyTbl> properties = ...;
IEnumerable<PropertyTbl> filteredProperties = AddressUtils.FilterAddresses(properties, "whatever", AddressSearchOptions.Whatever, p => p.Address);