简体   繁体   English

如何对待Linq to Entities查询?

[英]How to treat a Linq to Entities query?

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() ? 在调用First()之前检查文章是否为空的最佳方法是什么?

A try catch block or introduce a variable and check how many articles are there? 尝试捕获块或引入变量并检查其中有几篇文章?

Try .FirstOrDefault(); 试试.FirstOrDefault(); . It will return null if nothing is found. 如果未找到任何内容,它将返回null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM