实体框架LInq查询Lambda语法Where子句

时间:2016-01-21 21:37:53

标签: sql-server lambda linq-to-entities asp.net-core-mvc entity-framework-core

我正在尝试使用Include,ThenInclude和Where子句从数据库中生成正确的数据。当在where子句中检查Id属性的相等性时,这将正常工作。但是,如果我检查字符串的等效性它不起作用。

数据库:

Linq查询1:

public List<Company> GetGateways()
{
    return DbSet
        .Include(c => c.Elements).ThenInclude(e => e.Attributes)
        .Include(c => c.Elements).ThenInclude(e => e.ElementType).Where(o => o.Id == 1)
        .Include(o => o.Users)
        .ToList();
}

SQL输出:

   SELECT [a].[Id], [a].[Active], [a].[Created], [a].[ElementId], [a].

    [Key], [a].[Updated], [a].[Value]
    FROM [Attributes] AS [a]
    INNER JOIN (
        SELECT DISTINCT [c].[Id], [e].[Id] AS [Id0]
        FROM [Elements] AS [e]
        INNER JOIN (
            SELECT DISTINCT [c].[Id]
            FROM [Companies] AS [c]
            WHERE [c].[Id] = 1
        ) AS [c] ON [e].[CompanyId] = [c].[Id]
    ) AS [e] ON [a].[ElementId] = [e].[Id0]
    ORDER BY [e].[Id], [e].[Id0]

Linq查询2 :(不起作用)

public List<Company> GetGateways()
{
    return DbSet
        .Include(c => c.Elements).ThenInclude(e => e.Attributes)
        .Include(c => c.Elements).ThenInclude(e => e.ElementType).Where(o => o.Name == "Gateway")
        .Include(o => o.Users)
        .ToList();
}

SQL输出2:

SELECT [c].[Id], [c].[Active], [c].[Created], [c].[Name], [c].[Updated]
FROM [Companies] AS [c]
WHERE [c].[Name] = 'Gateway'
ORDER BY [c].[Id]

我的linq查询出错的任何想法?

0 个答案:

没有答案