c# - 通过 linq 比较无值

标签 c# linq

我在 sql server 2008 数据库/表中有一个整数列(非空),我想检索它。

  // go to the table and get the largest number, if none exists, use 0.
        int iNumber = iContext.DetailsRecords.Max(x => x.Number); // from entity

但一开始, table 是空的。我想设置为0。 如何更改代码?

最佳答案

如果您不想检查 DetailsRecords 是否为空,而是处理源序列是否为空,则使用此答案(我针对您的 LINQ 用法对其进行了一些不同的调整):

Max or Default?


int iNumber = iContext.DetailsRecords.Select(x => (int?)x.Number).Max() ?? 0;

关于c# - 通过 linq 比较无值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13349551/

相关文章:

c# - 唯一选择用户名

.net - 数据表过滤: linq vs filter?

c# - 在 LINQ 中加入和分组

c# - 将 .exe 打包成 .vsix 并从 Visual Studio 扩展中调用

c# - 将现有 pdf 表单字段替换为同一位置的新表单字段

c# - 调整大小后渲染到纹理失败

c# - 如何使用 visual studio 将某些行排除在构建之外?

c# - 使用 C# 使用 Selenium WebDriver 进行跨浏览器测试

c# - 如何从定义为接口(interface)的 IQueryable 获取 where 子句

c# - 如何在c#中转换表达式