Take the 2-minute tour ×

I have this code:

    public article GetArticleByWebSite(string webSite)
        using (var context = new ceopolandEntities())
            return context.article.Where(a => a.WebSite == webSite).First();

What's the best way to check if article isn't empty before calling First()?

A try catch block or introduce a variable and check how many articles are there?

share|improve this question
add comment

1 Answer

up vote 7 down vote accepted

Try .FirstOrDefault();. It will return null if nothing is found.

share|improve this answer
add comment

Your Answer


