【问题标题】:How to treat a Linq to Entities query?如何处理 Linq to Entities 查询?
【发布时间】:2009-10-29 22:05:57
【问题描述】:

我有这个代码:

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

在致电First() 之前检查文章是否为空的最佳方法是什么?

一个try catch块或引入一个变量并检查有多少篇文章?

【问题讨论】:

    标签: c# .net linq exception


    【解决方案1】:

    试试.FirstOrDefault();。如果没有找到,它将返回null。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      相关资源
      最近更新 更多