Là một giải pháp thay thế, bạn có thể sử dụng các phương pháp sau đây cho phép xác định một trong hai lĩnh vực lọc hoặc chức năng lọc:
var adresses = new List<Address>{
new Address() { City = "ABC", Country = "USA" },
new Address() { City = "BC", Country = "USA" },
new Address() { City = "C", Country = "UK" }
};
var filterValues = new List<string> { "B", "UK", "U" };
//
var FilterContains = [email protected]((values, value) => values.Contains(value));
var FilterStartsWith = [email protected]((values, value) => values.Any(v => value.StartsWith(v)));
//
var AdressesByCity = [email protected](a => a.City);
var adressesByCitiesContains = AdressesByCity(filterValues, FilterContains); // B->{ABC;USA},{BC;USA}
var adressesByCitiesStartsWith = AdressesByCity(filterValues, FilterStartsWith);// B->{BC;USA}
//
var AdressesByCountry = [email protected](a => a.Country);
var adressesByCountriesContains = AdressesByCountry(filterValues, FilterContains);//U,UK-> {C;UK}
var adressesByCountriesStartsWith = AdressesByCountry(filterValues, FilterStartsWith); //U,UK->{ABC;USA},{BC;USA}{C;UK}
Ở đây, phương pháp khuyến nông @Specify
được thực hiện như sau:
public static class @SpecifyExtension {
public static Func<IEnumerable<V>, Func<IEnumerable<V>, V, bool>, IEnumerable<U>> @Specify<U, V>(this IEnumerable<U> source, Func<U, V> selector) {
return (values, predicate) => source.Where(x => predicate(values, selector(x)));
}
public static Func<IEnumerable<TValue>, TValue, bool> @Specify<TValue>(this IEnumerable<TValue> source, Func<IEnumerable<TValue>, TValue, bool> func) {
return func; // for Type-inference only
}
}
Tôi nghĩ bạn nên xác định loại biến. –
Nó phụ thuộc vào kiểu dữ liệu 'myFilters'. Vui lòng mở rộng câu hỏi của bạn với thông tin này. –
@VadimMartynov Tôi không hiểu .... tại sao bạn cần biết loại dữ liệu myfilters – Viru