這對我來說可能有點棘手,因爲我是LINQ的新手。 我有以下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
});
動態LINQ可以解決你的問題。看看這篇Scott Gu的文章:http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx – 2013-04-30 16:03:16