Entity Framework - Any() v s count() with null

Asked By Ali Khan on 28-Feb-13 03:48 AM
var lstRoadmaps = from roadmap in SessionManager.Current.ProcessComplianceQueryFactory.GetAllRoadmaps()
select roadmap;
if (lstRoadmaps.Any()&& lstRoadmaps!=null)
{//

}

--------------------------------
var lstRoadmaps = from roadmap in SessionManager.Current.ProcessComplianceQueryFactory.GetAllRoadmaps()
select roadmap;
if (lstRoadmaps.Count()&& lstRoadmaps!=null)
{//

}
------------------------------------------------------
Q1
-what is the difference between the above two in functioanlity wise.
_______________________________________________________________________________________________________
var lstRoadmaps = from roadmap in SessionManager.Current.ProcessComplianceQueryFactory.GetAllRoadmaps()
select roadmap;
if (lstRoadmaps.Any()&& lstRoadmaps!=null)
{//

}

--------------------------------
var lstRoadmaps = from roadmap in SessionManager.Current.ProcessComplianceQueryFactory.GetAllRoadmaps()
select roadmap;
if (lstRoadmaps.Count()&& lstRoadmaps!=null)
{//

}
------------------------------------------------------
Q2 lstRoadmaps
!=null is necessary to check or any() & count() is enough to handle null



it
is very urgent please.
Danasegarane Arunachalam replied to Ali Khan on 28-Feb-13 08:50 PM
 Any()  is part of System.Linq.IEnumerable namespace and .count() is part of System.Collections.ObjectModel.Collection namespace.

.Any  : check for existence of a object inside a collection . If there any no objects it returns false and contains any object returns return true.

count() : Gets the number of items in the list.

If you want to check value exist then use .Any and want the count then use the .Count method.