LINQ - Linq Query - For Search Page - Asked By Shilpa Sharma on 31-Mar-11 10:17 AM

Hi,

I have a serach page in my application, which takes following inputs:
  • Forename
  • Surname
  • DOB
  • Address
In order to serach I can provide exact/partial values to all of the above parameters or to none of them. In this case how should I write my LINQ query to get the output of the respective search from the DB.
Outline of the query I am expecting:

var Details = from t1 in context.tblPatient
         where  (t1.Forename.Contains("<Input Forename Value>")  //this will be in serach criteria if Forename has input value
            and t1.Surname.Contains("<Input Surname Value>") //this will be in serach criteria, if Surname has input value
            and t1.DOB == <Input DOB Value> //this will be in serach criteria, if DOB has input value
            and t1..Address.Contains("<Input Address Value>") //this will be in serach criteria, if Address has input value
      select t1;

Thanx in Advance 
Shilpa

            
Riley K replied to Shilpa Sharma on 01-Apr-11 01:43 AM


  Rather than using AND condition in ur query use OR condition, this matches any one of the passed parameters and returns the result

var Details = from t1 in context.tblPatient
     where  (t1.Forename.Contains("<Input Forename Value>")  //this will be in serach criteria if Forename has input value
        || t1.Surname.Contains("<Input Surname Value>") //this will be in serach criteria, if Surname has input value
        || t1.DOB == <Input DOB Value> //this will be in serach criteria, if DOB has input value
        || t1..Address.Contains("<Input Address Value>") //this will be in serach criteria, if Address has input value
    select t1;
Jitendra Faye replied to Shilpa Sharma on 01-Apr-11 08:49 AM

IN YOUR CODE - you have used and operator for matchin condition, here you have to use or condition

var Details = from t1 in context.tblPatient
     where  (t1.Forename.Contains("<Input Forename Value>")  //this will be in serach criteria if Forename has input value
        and t1.Surname.Contains("<Input Surname Value>") //this will be in serach criteria, if Surname has input value
      and t1.DOB == <Input DOB Value> //this will be in serach criteria, if DOB has input value
      and t1..Address.Contains("<Input Address Value>") //this will be in serach criteria, if Address has input value
    select t1;


change these lines to following lines-

var Details = from t1 in context.tblPatient
     where  (t1.Forename.Contains("<Input Forename Value>")  //this will be in serach criteria if Forename has input value
      || t1.Surname.Contains("<Input Surname Value>") //this will be in serach criteria, if Surname has input value
      || t1.DOB == <Input DOB Value> //this will be in serach criteria, if DOB has input value
      || t1..Address.Contains("<Input Address Value>") //this will be in serach criteria, if Address has input value
    select t1;


use it and let me know.