c# - 无法从用法中推断出方法的类型参数。尝试指定类型参数 ex - 错误。任何指导?

标签 c# linq-to-sql tsql

    /// <summary>
    /// Find all of the Areas that are Parents without children Areas.
    /// </summary>
    /// <returns>IQueryable object full of Areas.</returns>
    public IQueryable FindParentAreas()
    {
        return db.Areas.SelectMany(x => x.ParentAreaID == null);
    }

我想返回作为父区域的区域对象的集合,这意味着它们在 ParentAreaID 字段(在我的数据库中)中具有空值。

看来我不能只将它与 null 进行比较,因为 C# 中的 null 可能在 Microsoft SQL Server 中表示其他含义。

有什么指导吗?我应该使用 SelectMany 吗?

最佳答案

Enumerable.SelectMany :

Projects each element of a sequence to an IEnumerable<T> and flattens the resulting sequences into one sequence.

Enumerable.Where :

Filters a sequence of values based on a predicate.

我认为您正在寻找Where:

public IQueryable<Area> FindParentAreas()
{
    return db.Areas.Where(x => x.ParentAreaID == null);
}

关于c# - 无法从用法中推断出方法的类型参数。尝试指定类型参数 ex - 错误。任何指导?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3071235/

相关文章:

C# Lambda 表达式未返回预期结果

tsql - 如何在 T-SQL 中用年、月、日计算年龄

c# - 3DES 加密问题 - 每次都有不同的密文

c# - T4 模板 : Import namespace in host assembly

c# - 使用 LinqToSql 生成返回数据库记录?

SQL Server 单位问题

sql - 在 SQL Server 2008 中模拟没有自定义函数的 STRING_SPLIT

c# - 如何将日期时间转换为十六进制

C# 以非管理员权限运行进程

linq-to-sql - Linq 到 Sql : Can I return the Identity_Scope after an insert?