0

这对我来说可能有点棘手,因为我是 LINQ 的新手。我有以下 LINQ 代码正确返回我的数据。但是,给出的条件是有选择性的。意味着,所有条件可能会或可能不会一次给出。条件是基于用户输入的。那么,如何根据用户选择的标准编写这种增量 LINQ。

        var dRows = (from TblPatientInformation in this.dsPrimaPlus1.tblPatientInformation
                    join TblDoctorMaster in this.dsPrimaPlus1.tblDoctorMaster on new { PtI_dcMId = Convert.ToInt32(TblPatientInformation.ptI_dcMId) } equals new { PtI_dcMId = TblDoctorMaster.dcM_Id }
                    join TblDepartmentMaster in this.dsPrimaPlus1.tblDepartmentMaster on new { DcM_deptMId = TblDoctorMaster.dcM_deptMId } equals new { DcM_deptMId = TblDepartmentMaster.ID }
                    join TblPatientDiagnosis in this.dsPrimaPlus1.tblPatientDiagnosis on new { PtI_Id = TblPatientInformation.ptI_Id } equals new { PtI_Id = Convert.ToInt32(TblPatientDiagnosis.ptD_ptIId) }
                    join TblDiagnosisInformation in this.dsPrimaPlus1.tblDiagnosisInformation on new { PtD_tgIId = Convert.ToInt32(TblPatientDiagnosis.ptD_tgIId) } equals new { PtD_tgIId = TblDiagnosisInformation.tgI_Id }
                    where
                      TblPatientInformation.ptI_Id > 0 ||
                      TblPatientInformation.ptI_PatientName.Contains(txtName.Text)  ||
                      TblPatientInformation.ptI_Code == int.Parse( txtCaseNo.Text) ||
                      TblDepartmentMaster.ID ==int.Parse( cmbDepartment.SelectedValue.ToString()) ||
                      TblDoctorMaster.dcM_Id == int.Parse(cmbDoctor.SelectedValue.ToString()) ||
                      TblDiagnosisInformation.tgI_Id == int.Parse(cmbDiagnosis.SelectedValue.ToString())
                    select new
                    {
                        TblPatientInformation.ptI_Id,
                        TblPatientInformation.ptI_Code,
                        TblPatientInformation.ptI_PatientName,
                        TblPatientInformation.ptI_dcMId,
                        TblPatientInformation.ptI_Age,
                        TblPatientInformation.ptI_Address,
                        TblPatientInformation.ptI_eMail,
                        TblPatientInformation.ptI_Phone1,
                        TblPatientInformation.ptI_Phone2,
                        TblPatientInformation.ptI_Phone3,
                        TblPatientInformation.ptI_Date,
                        TblPatientInformation.ptI_Gender,
                        TblDiagnosisInformation.tgI_Name,
                        TblDiagnosisInformation.tgI_Description,
                        TblDoctorMaster.dcM_FullName,
                        TblDepartmentMaster.Department
                    });
4

2 回答 2

1

为此,我建议试用 Predicate Builder http://www.albahari.com/nutshell/predicatebuilder.aspx 。

该帖子建议以下内容:

试验 PredicateBuilder 最简单的方法是使用 LINQPad。LINQPad 让您可以立即针对数据库或本地集合测试 LINQ 查询,并且直接支持 PredicateBuilder(按 F4 并选中“Include PredicateBuilder”)。

这是一种使用这种方法的简单方法。

希望有帮助。

于 2013-04-30T16:18:19.940 回答
0

一种解决方案是使用动态 LINQ,您可以在其中指定字符串表达式,而不是代码表达式。例如

// Dynamic Linq string expression
var result = context.People.Where("Age >= 3 And StreetNumber < 3").ToList();

相对于:

// Linq code expression
var result = context.People.Where(q=>q.Age>=3 && q.StreetNumber < 3).ToList();

有了这个,您可以根据用户输入解析您的表达式,例如

StringBuilder sb = new StringBuilder();
...
if(criteria1)
{
  sb.Append(" And Criteria>1");
}
...
if(criteria2)
{
  sb.Append(" And Criteria2<15");
}
...
var result = context.People.Where(sb.ToString()).ToList();

查看此 Scott Gu 文章以获取完整示例:

http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

于 2013-04-30T16:11:53.997 回答