c# - 第一的?单例的?还是默认?

标签 c# linq entity-framework

我搜索了 StackOverflow,但仍然无法理解使用单一、第一(或默认)查询的正确语法。

我愿意先查询特定语言的翻译。如果数据库上没有这样的翻译,就拿第一个英文的。

这是我到目前为止得到的:

locale = "ja-jp";

var items = from c in db.Contents.Include("Translation")
            where c.RegionalInfo.Any(x => x.RegionId == locale)
            select c;

编辑 Obs.: items 是一个 IEnumerable

最佳答案

尝试这样的事情。

var item = (from c in db.Contents.Include("Translation")
        where c.RegionalInfo.Any(x => x.RegionId == locale)
        select c).FirstOrDefault();
if (item != null)
   ...

关于c# - 第一的?单例的?还是默认?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7325218/

相关文章:

asp.net-mvc - 新手 - 将父 View 和 subview 合并到一个页面中

c# - 如何使用我的个人电脑在 windows iis 服务器上发布我的网站?

c# - 为这个聊天应用程序使用 Ajax(可能与 Web 服务一起使用)

c# - 在列表对象中排序

c# - 多参数linq表达式如何初始化它们的参数?

c# - Entity Framework ,如何投影到列表中

c# - 如何使 ValidatesOnDataErrors 可绑定(bind)?

c# - 使用 Assembly.GetType ("MyCompany.Class1.Class2") 返回 null

c# - ASP.NET Core EF - 上传文件时出错

c# - 是否为 DbSet 启用了删除级联?