Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Dear all,

I am writing to seek help, in how do combine two linq queries into using if condition.
C#
public bool full(string username, string password)
        {

            var query1 = //query logic

            // "execute" the query
            return query1.FirstOrDefault() != null;
        }


        public bool trial(string username, string password)
        {

            var query2 = // query logic

            // "execute" the query
            return query2.FirstOrDefault() != null;
        }</pre>


Can i do this, using action name parameter to on each query, if not, can anyone suggest any framework or guide i could use, to implement this task.
Many thanks.
Posted
Comments
Vedat Ozan Oner 13-Mar-14 9:20am    
I couldn't understand. Can you explain more what to do?
miss786 13-Mar-14 9:52am    
thank you for your response. the two queries above, output two different user group and i would like to combine the both queries using a condition such as:
if (user = query1)
{
display page 1
}
else if (user = query 2)
{
display page 2
}
else
{
display page 3
}

apology for the confusion and hope my explanation clarifies my issue. Many thanks for your help and time.
Vedat Ozan Oner 13-Mar-14 10:32am    
then you have just found your solution. by including user group in your query, you can direct user to an appropriate page according to his/her group. therefore you don't need two separate methods like 'full' or 'trial'.
miss786 13-Mar-14 10:45am    
thank you so much for your response and help. if possible, could you provide some example to the user group function. Many thanks for clarifying that up.

1 solution

can be like that:


assuming your user table:
SQL
create table USER (USERNAME nvarchar(10), PASSWD nvarchar(10), int USERGROUP)


C#
void AuthenticateUser(string username, string pwd) {
  var q = from u in MyContext.USERs
    where u.USERNAME==username && u.PASSWD==passwd
    select u.USERGROUP;
  var usergroup = q.FirstOrDefault();
  if(usergroup == null)
    Page.Redirect("NotFound");
  
  switch(usergroup) {
    case 1:
      Page.Redirect("usergroup1_page");
      break;
    case 2:
      Page.Redirect("usergroup2_page");
      break;
    default:
      Page.Redirect("error_page");
      break;
  }
}


Please note that I didn't test this code. I hope the idea is clear.
 
Share this answer
 
Comments
miss786 13-Mar-14 11:06am    
This is great help. Many thanks. Have a great day. =D
Vedat Ozan Oner 13-Mar-14 11:07am    
you're welcome :)

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