c# - 如何使用参数中的函数作为过滤器?
问题描述
我想从接口调用这个函数。
ICollection<Store> GetStores(Func<Store, bool> filter, bool includeCustomers = false);
你会怎么称呼它?它需要一个过滤功能,我不知道如何使用。
var returnStores = IRepository.GetStores(/*what to write here*/);
例如要查找具有 storeID 的商店:
public class Store
{
public int StoreId { get; set; }
public string CountryCode { get; set; }
public ICollection<Customer> Customers { get; set; }
}
解决方案
您可以传入与所需签名匹配的现有方法的名称:接收 aStore
并返回布尔值的方法。
否则,您可以像这样传递匿名函数:
var returnStores = IRepository.GetStores(store => store.Id == 42);
匿名函数接收 aStore
并true
仅在其Id
为 42 时返回。这只是一个示例,关键是lambda 必须返回一个 bool。
推荐阅读
- wechat - 没有ICP授权域名可以开始开发wehcat小程序和测试服务器吗?
- python - 从多个 Python 池中修改列表
- powershell - $PROFILE 是控制台和 VS Code 终端之间的不同类型
- android - Jetpack Compose 1.0.4 预览和运行时错误
- c# - 如何确保 Process.Start 不会抛出 Win32Exception "File not found"
- react-native - 当我调用 ethers.Wallet.fromMnemonic() [Ethers js 5.5.1] 时,React Native 缓慢/冻结
- python - 编写此函数的正确方法是什么?
- python - Pytesseract 不断收到语法或路径错误
- python - 仅使用单个列表输入填充矩阵
- android-studio - 如何从 Android Studio Gradle 建议中排除 SNAPSHOT 或其他依赖项