Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Jun 29, 2012 02:49 PM by coolblue
Participant
1087 Points
502 Posts
Jun 29, 2012 09:20 AM|LINK
I have the following linq code
justificationList=(from j in context.Justifications where j.commentDate>=fromdate select j).DefaultIfEmpty().ToList();
However if there are no items found it returns a list with one item in it that is null. How do I get it to return an empty list instead?
All-Star
87431 Points
9717 Posts
Moderator
MVP
Jun 29, 2012 10:09 AM|LINK
Change your code to do this:
justificationList=(from j in context.Justifications where j.commentDate>=fromdate select j);
if(justificationList == null)
justificationList = new List<Justification>();
Member
506 Points
78 Posts
Jun 29, 2012 10:15 AM|LINK
justificationList=(from j in context.Justifications where j.commentDate>=fromdate select j).ToList();
Jun 29, 2012 02:49 PM|LINK
@Rajaji.K
Thanks, for some reason I thought that threw an exception if no records were found.... I was clearly wrong ;-)
coolblue
Participant
1087 Points
502 Posts
return empty list with defaultifempty()
Jun 29, 2012 09:20 AM|LINK
I have the following linq code
justificationList=(from j in context.Justifications where j.commentDate>=fromdate select j).DefaultIfEmpty().ToList();
However if there are no items found it returns a list with one item in it that is null. How do I get it to return an empty list instead?
DarrellNorto...
All-Star
87431 Points
9717 Posts
Moderator
MVP
Re: return empty list with defaultifempty()
Jun 29, 2012 10:09 AM|LINK
Change your code to do this:
justificationList=(from j in context.Justifications where j.commentDate>=fromdate select j);
if(justificationList == null)
justificationList = new List<Justification>();
Darrell Norton's Blog
Please click "Mark as Answer" if this helped you.
Rajaji.K
Member
506 Points
78 Posts
Re: return empty list with defaultifempty()
Jun 29, 2012 10:15 AM|LINK
coolblue
Participant
1087 Points
502 Posts
Re: return empty list with defaultifempty()
Jun 29, 2012 02:49 PM|LINK
@Rajaji.K
Thanks, for some reason I thought that threw an exception if no records were found.... I was clearly wrong ;-)