c# - C# Razor Linq,一个产生意外结果的通用变量
问题描述
你能解释一下我在这里做错了什么吗
我在 linq 中使用搜索条件,并且在过滤结果时遇到了这个问题,如果我使用了一个通用变量,那么它不会按预期进行搜索。
我使用了单独的变量并且它有效
工作正常:
string city = Request.QueryString["city"];
properties = (Request.QueryString["city"] != null) ? properties.Where(x => x.City == city) : properties;
string pType = Request.QueryString["propertytype"];
properties = (Request.QueryString["propertytype"] != null) ? properties.Where(x => x.PropertyType == pType) : properties;
当我使用一个通用变量时不起作用:
string searchCriteria = Request.QueryString["city"];
properties = (Request.QueryString["city"] != null) ? properties.Where(x => x.City == searchCriteria) : properties;
searchCriteria= Request.QueryString["propertytype"];
properties = (Request.QueryString["propertytype"] != null) ? properties.Where(x => x.PropertyType == searchCriteria) : properties;
还有使多重搜索更加优化的任何策略。
解决方案
我认为如果你不使用变量会更好,你可以做这样的事情
if(!string.IsNullOrEmpty(Request.QueryString["city"]))
{
properties =properties.Where(x => x.City == Request.QueryString["city"]);
}
if(!string.IsNullOrEmpty(Request.QueryString["propertytype"]))
{
properties = properties.Where(x => x.PropertyType == Request.QueryString["propertytype"])
}
推荐阅读
- firebase - 如何等待最里面的firebase函数的响应?
- python - Altair mark_line plots noisier than matplotlib?
- javascript - 登录成功后如何更新 ApolloClient 授权标头?
- python - 这是在同一视图中处理多个表单集的好方法吗?
- php - 如何将php变量转换为mp3
- javascript - 如何在开始执行之前第一次在Javascript中执行for循环时设置1s延迟
- ajax - 我无法将在我的 Flask 服务器中创建的 python 字典返回到我的 Javascript HTML 页面
- ios - 在 Swift 中打开我的 iOS 应用程序的文档目录
- java - 如何为tomcat 8上的特定api调用禁用ssl连接?
- android - 缩放 Imageview 画布绘制位置后更改如何修复它