【发布时间】:2020-05-04 17:30:03
【问题描述】:
我正在尝试使动态 order by 子句起作用,但无法弄清楚语法。以为我会用内联 if 语句破解它,但 EF 不支持这些。
用户可能通过单击 dgv 标题来选择要排序的字段(最初是升序)。
这是我的代码:-
Dim q = (From customer In db.tblcustomers.Where(Function(x) CBool(x.f_rn >= 0 And x.cu_brn = "00" _
And (txtFiltAccno.Text.Trim = "" Or x.cu_accno.ToUpper.Contains(txtFiltAccno.Text.ToUpper.Trim)) _
And (txtFiltCustName.Text.Trim = "" Or x.cu_name.ToUpper.Contains(txtFiltCustName.Text.ToUpper.Trim)) _
And (txtFiltPhone.Text.Trim = "" Or x.cu_telno.ToUpper.Contains(txtFiltPhone.Text.ToUpper.Trim)) _
And (txtFiltFax.Text.Trim = "" Or x.cu_faxno.ToUpper.Contains(txtFiltFax.Text.ToUpper.Trim)) _
And (txtFiltEmail.Text.Trim = "" Or x.cu_email.ToUpper.Contains(txtFiltEmail.Text.ToUpper.Trim))))
select customer.f_rn, customer.cu_accno, customer.cu_name, customer.cu_add1, customer.cu_add2, customer.cu_add3, customer.cu_add4, customer.cu_add5,
customer.cu_telno, customer.cu_faxno, customer.cu_email).OrderBy(Function(u)
IIf(a = "cu_name", u.cu_name,
IIf(a = "cu_add1", u.cu_add1,
IIf(a = "cu_add2", u.cu_add2,
IIf(a = "cu_add3", u.cu_add3,
IIf(a = "cu_add4", u.cu_add4,
IIf(a = "cu_add5", u.cu_add5,
IIf(a = "cu_telno", u.cu_telno,
IIf(a = "cu_faxno", u.cu_faxno,
IIf(a = "cu_email", u.cu_email, u.cu_accno)))))))))).Skip((pagenum - 1) * 25).Take(25)
【问题讨论】:
-
谢谢,但用户可能希望根据多个条件进行搜索,例如显示在两个日期之间订购并发送到特定区域的所有蓝色小部件。
-
另外我将查询限制为 25 条记录,因此在 dgv 上排序只会对这 25 条记录进行排序,而不是对整个表进行排序。据我所知,它必须是回到数据库的一次旅行,尤其是同时可能已经添加了新记录
-
抱歉,我刚刚重读了您的消息,只是将查询分解,但要对查询运行分析器以查看它在做什么
标签: vb.net linq frameworks entity