开发者

How to treat a Linq to Entities query?

开发者 https://www.devze.com 2022-12-10 07:05 出处:网络
I have this code: public article GetArticleByWebSite(string webSite) { using (var context = new开发者_如何学运维 ceopolandEntities())

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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消