Ravi
Ravi

Reputation: 307

LINQ query should be based on criteria

This might be bit tricky here for me, as I am new to LINQ. I have following LINQ code returns me data properly. But, the conditions given are selective. Means, all the conditions may or may not be given at a time.Conditions are user input based. So, how to write such incremental LINQ based on criteria selected by user.

        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
                    });

Upvotes: 0

Views: 123

Answers (2)

David Tansey
David Tansey

Reputation: 6023

I recommend trying out Predicate Builder http://www.albahari.com/nutshell/predicatebuilder.aspx for this purpose.

That post recommends the following:

The easiest way to experiment with PredicateBuilder is with LINQPad. LINQPad lets you instantly test LINQ queries against a database or local collection and has direct support for PredicateBuilder (press F4 and check 'Include PredicateBuilder').

which is an easy way to get going with this approach.

Hope that helps.

Upvotes: 1

George Johnston
George Johnston

Reputation: 32258

One solution would be to use Dynamic LINQ, where you can specify string expressions, instead of code expressions. e.g.

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

as opposed to:

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

With this, you can parse your expression based on the user input, e.g.

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();

Check out this Scott Gu article for a complete example:

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

Upvotes: 0

Related Questions