Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In sql I use like this:
Classcud2 ob1 = new Classcud2("select * from tbl_usertotalrating where store_name='" + TxtCompany.Text + "'");
        if (ob1.ds.Tables[0].Rows.Count > 0)
        {
           // what i want
        }
        else
        {
         // what i want
        }

but my problem is I want to use "if" "else" condition in linq,
In linq :
var v = from k in li.category1s where k.cat_name == Ddlcategory.Text select k;

but whether this condition is right or wrong "var v " always have some data,
then how i use like this
C#
if(v==null)
{

}
else
{

}

I thing you understand my problem,
please help me
Posted
Updated 3-Jan-13 18:27pm
v2

Use like this

C#
if (v.Count() > 0)
 {
   //what you want
 }
 else
 {
   //what you want 
 }
 
Share this answer
 
Comments
_Amy 4-Jan-13 1:05am    
5'ed !!
Arun kumar Gauttam 4-Jan-13 2:04am    
thanks
When no results are returned, you have an 'empty' list. This is same as a new list created.

For it, you can try any of the following:
C#
if (!returnedResult.Any()) 
{
  // No result
}

//OR
if(returnedResult.Count() > 0)
{ 
  // Some result
}

//OR 
// convert result into a list
var returnedResult = (from Q in myDataContext select Q.ID).ToList();
if (returnedResult.Count > 0)
{
  // Some data returned
} 
 
Share this answer
 
v2
Comments
_Amy 4-Jan-13 1:05am    
5'ed !!
Sandeep Mewara 4-Jan-13 10:39am    
Thanks.
Arun kumar Gauttam 4-Jan-13 2:04am    
thanks
Sandeep Mewara 4-Jan-13 10:39am    
Welcome.
This is a where, so you should use a strongly typed list. Then use ToList() and you can check if your list has more than one item in it. Or just do a for each on var, and in side the for each, set a bool to true, that will mean you have data.
 
Share this answer
 

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