c# - 过滤 IQueryable 会带回错误的结果
问题描述
我有一个接受IQueryable<T>
作为参数的方法,称为attachments
. 在此方法中,我在多个语句中进一步过滤查询if
。所以我的代码如下:
if(firstCondition)
{
attachments = attachments.Where(i => i.TestItemId == 1); //3 records in db
DoWork(attachments);
}
if(secondCondition)
{
attachments = attachments.Where(i => i.TestItemId == 2); //2 records in db
DoWork(attachments);
}
...
我在里面DoWork();
做:
foreach(var items in attachments)
{
//write attachment name to file here
}
在数据库中,我总共有 5 条记录,在第一条if
语句中我得到了适当的结果。但是在第二种if
情况下,我在查询中得到 0 结果。有人可以告诉我哪里出错了。
请注意,两个 if 条件都为真。
解决方案
您不应该为 firstCondition 分配附件,结果将按 2 个条件过滤:TestItemId == 1 && TestItemId == 2。=> 它总是返回空列表;
推荐阅读
- python - 从浮点值列表创建 DataFrame
- c# - 具有多个电子邮件地址的正则表达式,由分号分隔
- acumatica - Acumatica 发现什么报告使用什么成本计算方法
- javascript - 为什么附加到 div 的 onclick 处理程序停止触发?
- java - 将数据传递给 RecyclerView 的空引用问题
- java - 在微服务中使用 Ribbon 作为 LoadBalancer
- angular - API Laravel 5.8 和 Angular 7 中的错误 500(内部服务器错误)
- html - 如何将从 powershell 脚本返回的多个值填充到 HTML 下拉列表中?
- java - 动态制作同一实体的多个表
- kubernetes - 是否可以使用 Kubernetes 部署 GUI 应用程序?