Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if a variable is empty c#

I have a pretty basic question but it is doing my nut in!!!!

I have created a variable which checks my data table checks to see if an item using my page control ID already exists. IF it does I then want to warn my user that they have already chosen a page colour!

My question is how do I check if this variable is empty or not!

var qry = from x in db.DT_Control_ColourPalette_PageColors
                  where x.PageControlID == int.Parse(HF_CPID.Value)
                  select new
                  {
                      x.PageControlID,
                  };

The argument I think is right?

if (qry !=null)
like image 522
Callum Avatar asked Jun 17 '11 08:06

Callum


2 Answers

Query expressions don't return null as far as I know. If there are no results you just get an IQueryable<T> with no Ts inside.

You can use this instead to see if there's anything in the result set:

if (qry.Any())
like image 92
BoltClock Avatar answered Sep 21 '22 06:09

BoltClock


presuming that should return a single value - if so, then:

var qry = (from x in db.DT_Control_ColourPalette_PageColors
                  where x.PageControlID == int.Parse(HF_CPID.Value)
                  select new
                  {
                      x.PageControlID,
                  }).FirstOrDefault();

if(qry != null)
{
   // do stuff
}
like image 42
Nathan Avatar answered Sep 23 '22 06:09

Nathan