Tried to look for this question on SO, but couldn't find it.
What's the best way to return a list of records from a static method?
I want to return either an empty list or a populated list from my static method.
Given the following method:
public static List<division> GetAllDivisions(bool isAvailable)
{
MyDataContext db = new MyDataContext ();
List<division> DivisionList = new List<division>();
var data = from d in db.divisions
where d.isAvailable == isAvailable
select d;
if(data.Count() > 0)
DivisionList = data.ToList();
return DivisionList;
}
Do I really need to do the following?
if(data.Count() > 0)
DivisionList = data.ToList();
Can I just do DivisionList = data.ToList()
without checking the count?
I want to be able to return either a populated list or an empty list - and I don't want an error thrown if there are 0 records.
What are best practices? Is it better to return IEnumerable
?