c# - dbml linq 中 where 子句中比较日期的问题
问题描述
我正在使用 DBML 在我的应用程序中进行 linq 搜索。DynamicLibrary.cs
我设计了与本文相关的高级搜索查询:
使用 Linq 进行动态查询
所以我创建了一个 dbml 并将其用于我的高级 linq 搜索,而无需额外连接到数据库。除了数据类型,我对数据类型没有任何问题date
。
这是我whereClause()
使用它的功能。
private string whereClause()
{
string strWhere = string.Empty;
if (rbvalveStreet.Checked)
{
if (!string.IsNullOrEmpty(valveStreet.Text))
{
strWhere += " valveStreet.Contains(\"" + valveStreet.Text + "\") AND ";
}
}
if (rbhealth.Checked)
{
strWhere += " health = " + health.SelectedIndex.ToString() + " AND ";
}
if (rbleak.Checked)
{
strWhere += " leak = " + leak.SelectedIndex.ToString() + " AND ";
}
if (chbDateRange.Checked)
{
string fromDate = clsEasy.makeDate8((DateTime)dtsFrom.Value); //ex: 2018/07/30
string toDate = clsEasy.makeDate8((DateTime)dtsTo.Value); //ex: 2018/08/05
strWhere += " washdate >= " + fromDate + " && washdate <= "+ toDate + " AND ";
}
string[] remove = { " AND " };
foreach (string item in remove)
{
if (strWhere.EndsWith(item))
{
strWhere = strWhere.Substring(0, strWhere.LastIndexOf(item));
break; //only allow one match at most
}
}
return strWhere;
}
我想strWhere
在检查 chbDateRange 时添加到 linq 查询,并在日期之间进行搜索。但我收到此错误:列的数据类型也是
,我尝试以下代码:washdate
Date
DateTime fromDate = DateTime.Parse(clsEasy.makeDate8((DateTime)dtsFrom.Value));
DateTime toDate = DateTime.Parse(clsEasy.makeDate8((DateTime)dtsTo.Value));
strWhere += " washdate >= " + fromDate + " && washdate <= "+ toDate + " AND ";
我确定这里发生了数据类型冲突。请帮我解决这个问题。谢谢。
编辑:
clsEasy.makeDate8((DateTime)dtsFrom.Value) retrun date as string for example "2018/07/30"
编辑 2:
这就是我对 Linq DBML 使用 whereClause() 的方式:
private void btnFind_Click(object sender, EventArgs e)
{
string wClause = this.whereClause();
if (!string.IsNullOrEmpty(wClause))
{
string address = "Data Source=.;Initial Catalog=valveManagement2018;Integrated Security=True";
dgv.DataSource = null;
refreshData();
contextLinqDataContext cxt = new contextLinqDataContext(address);
var query = cxt.tblValveExpirations
.Where(wClause);
_dt = null;
_dt = LINQToDataTable(query);
refreshForm();
countRows();
}
else
{
refreshData();
refreshForm();
countRows();
}
}
解决方案
您无需创建 SQL 字符串即可动态添加过滤器。您可以链接多个 .Where() 调用来创建等效的 .Where() 调用(Condition1 && Condition2 && Condition3)
。
假设原始查询存储在一个名为的变量中,query
您可以编写如下内容:
IQueryable<MyEntity> query = ....;
if (rbvalveStreet.Checked && !string.IsNullOrEmpty(valveStreet.Text))
{
var searchText=valveStreet.Text;
query=query.Where(item=>item.valveStreet.Contains(searchText);
}
if (rbhealth.Checked)
{
//Do we really want the *indexes*?
//SHouldn't this be the selected text/value?
var healthIdx=health.SelectedIndex;
query = query.Where(itme=>item.health = healthIdx);
}
if (rbleak.Checked)
{
var leakIdx=leak.SelectedIndex;
query = query.Where(item=>item.leak = leakIdx);
}
if (chbDateRange.Checked)
{
//No need to cast if this is a DateTimePicker control
DateTime fromDate = dtsFrom.Value;
DateTime toDate = dtsTo.Value;
query = query.Where(item=> item.washdate >=fromDate
&& item.washdate<= toDate)
}
决赛query
将所有指定的标准与AND
运算符相结合
推荐阅读
- node.js - 如何在 node.js express 应用程序中的 web 应用程序中创建多个登录名。管理员和客户
- javascript - ex的任何字符。'a' 应该只在 asp.net 文本框中输入一次
- php - Laravel Framework 5.7.27 自定义方法返回空白页
- javascript - NodeJS 的 TypeScript ECMAScript 目标
- html - 滚动时如何使按钮固定在底部
- rust - 在具有泛型的数据类型中未实现大小错误
- mariadb - java.sql.SQLException: Host 'xx.xx.xxx.xxx' is not allowed to connect to this MariaDB server
- azure - Azure Key Vault 证书未与 Web 应用自动同步
- vector - 在参考帧之间找不到连接路径
- javascript - 拖动图像时停止 Flash