Click here to Skip to main content
15,887,444 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi all,
i am using asp.net mvc3.My task is to validate using if clause in linq query.
Could any one please help me out in this

This is my code

C#
public JsonResult Details(string FirstName,string LastName,string City,string State,string Country,
   string Company,string College)
       {
           if (FirstName != null || LastName != null || City != null || State != null || Country != null || Company != null ||  College != null)

               var st = (from i in dbContext.User_details
                         join j in dbContext.User_addresses on i.Userid equals j.Userid
                         join k in dbContext.Userworkdetails on j.Userid equals k.Userid
                         join l in dbContext.Useredudetails on k.Userid equals l.Userid

    where i.Firstname==FirstName && i.Lastname==LastName && j.City==City && j.State==State && j.Country==Country
    && k.Company==Company && l.College==College
                         select new
                         {

                             Id = i.Userid,
                             city = j.City,
                             Firstname = i.Firstname,
                             company = k.Company,
                             college = l.College,
                             state = j.State,
                             lastname = i.Lastname
                         }).ToArray();
               return Json(st, JsonRequestBehavior.AllowGet);
       }


And here if the user enters value in any of the fields in form page they must validated using if clause and if they are present in the data base all the related values must be returned
I am facing problem when am using this If clause in the query
Could any one please please help me
Thanks
Posted
Updated 14-May-12 1:19am
v2

1 solution

Your if statement and query doesn't make much sense. You are saying 'if any of these are not null' ... but what if say FirstName is provided but all the rest are null? Your query would still try to include the null values in the where clause.

You can break down your where into multiple steps, like so...

C#
// Setup the main query
var query = (from i in dbContext.User_details
             join j in dbContext.User_addresses on i.Userid equals j.Userid
             join k in dbContext.Userworkdetails on j.Userid equals k.Userid
             join l in dbContext.Useredudetails on k.Userid equals l.Userid

if (!string.IsNullOrEmpty(FirstName))
     query = query.Where(u => u.FirstName == FirstName);

if (!string.IsNullOrEmpty(LastName))
     query = query.Where(u => u.LastName == LastName);

// TO DO, add in each of your possible filter options..

var results = from r in query.ToList()
              select new
                          {

                              Id = i.Userid,
                              city = j.City,
                              Firstname = i.Firstname,
                              company = k.Company,
                              college = l.College,
                              state = j.State,
                              lastname = i.Lastname
                          }).ToArray();


This way, only the values that are actually provided are used to filter the results. Alternatively, you could create a number of Overloads for each of the possible values, rather than passing null values.

Personally, I'd create a 'SearchCriteria' object that I pass in to the function, e.g.

C#
public JsonResult Details(SearchCriteria criteria)


Your criteria object will have all of the possible values that you can search by.
 
Share this answer
 
Comments
Sahithi Pinisetty 15-May-12 2:23am    
Thanks for the solution
I implemented your code but it is showing error in the if clause stating that "query body must end with a select clause or group clause" Could you please tell me why is this bug coming and how to fix it.
Thanks
Sahithi Pinisetty 19-May-12 8:38am    
@Dylan Morley: ThankYou so much....Its working :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900