views:

30

answers:

1

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 chceck if article isn't empty before calling First() ?

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

+5  A: 

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

Daniel A. White