Click here to Skip to main content
15,904,155 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
When i am using the following query in my code i am getting compilation error stating that "a query body must end with a select clause or group clause"
Could any one please help me out where i went wrong syntactically

This is my query

C#
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); 

        

        var results = from r in query.ToList()
                      select new
                                  {
                                      
                                     
                                      Firstname = i.Firstname,
                                      lastname = i.Lastname
                                  }.ToArray();


Thanks
Posted
Updated 14-May-12 23:50pm
v2

1 solution

Please try this
C#
var results = 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(u => u.FirstName == 
                !string.IsNullOrEmpty(FirstName) ? Firstname : LastName)
        select new {
                 Firstname = i.Firstname,
                 Lastname = i.Lastname
        }.ToArray();


It may be helpful
 
Share this answer
 
Comments
Sahithi Pinisetty 15-May-12 6:25am    
Actually in my code i have nearly 10 fields.The above code will be valid with only two values i.e firstname and lastname i suppose
Maciej Los 15-May-12 10:22am    
Yes, but you can change it. It's an example.
Maciej Los 15-May-12 10:22am    
Very good answer, my 5!
VJ Reddy 15-May-12 10:25am    
Thank you very much, losmac :)

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