实体框架核心Lambda Expression连接4个表并使用where过滤

时间:2016-10-14 02:15:00

标签: c# entity-framework lambda

我有六张桌子,如下图所示。我正在尝试根据Application_nameEnvironment_nameStatus获取数据。

状态表:

Id, Name

申请表:

Id, Name

服务器表:

Id, ServerName, Status

环境表:

Id, Name

ResourceGroup 表:

Id, Name, Application_Id, Environment_Id

ServersResourceGroup:

Id, Server_Id, Resource_Id

我要做的是加入所有需求表,并使用Application_nameEnvironment_nameStatus

过滤数据的位置

以下是我构建的查询,它通过过滤Application_name返回所有数据,但我无法通过Environment_nameStatus添加其他过滤器来满足上述要求: (

以下是返回Application_name

所有数据的查询
public IEnumerable<ResourceGroup> GetAllServersByApplication(string application_name, string environment_name, string status)
{
    var query = _context.ResourceGroup
            .Include(a => a.Application)
            .Include(t => t.Type)
            .Include(e => e.ServersGroup).ThenInclude(e => e.Environment)
            .Include(s => s.ServersGroup).ThenInclude(s => s.Server)
            .Include(s => s.ServersGroup).ThenInclude(s => s.Server).ThenInclude(s => s.Status)
            .Where(a => a.Application.Name == application_name)
            .ToList();

    return query;
}

以下是我尝试编写的查询,它将基于所有三个过滤器进行过滤:

    public IEnumerable<ResourceGroup> GetAllServersByApplication(string application_name, string environment_name, string status)
    {
        var query = _context.ResourceGroup
            .Include(a => a.Application)
            .Include(t => t.Type)
            .Include(e => e.ServersGroup).ThenInclude(e => e.Environment)
            .Include(s => s.ServersGroup).ThenInclude(s => s.Server)
            .Include(s => s.ServersGroup).ThenInclude(s => s.Server).ThenInclude(s => s.Status)
            .Where(a => a.Application.Name == application_name)
            .Select(e => e.ServersGroup.Where(s => s.Environment.Name == environment_name && s.Server.Status.Name == status)
            .ToList();

        return query;
    }

我在return query下面有一个红线。请看下面的图片: enter image description here

有没有更简单的方法来编写lambda查询然后我想要做什么?

非常感谢任何帮助。 :)

谢谢,

1 个答案:

答案 0 :(得分:1)

出现错误是因为您现在返回的ServersGroup集合与方法签名中的集合不同。这是因为您从ServersGroup添加了Select子句和选定的属性ResourceGroup。过滤应该在Where子句中进行。我更新了下面的代码,并将您的过滤器移到了SelectWhere,也包含了更改为Any的地方,作为Where expect boolean子句。

如果您打算在ServersGroup中仅包含某些ResourceGroup,那么您应该在连接或外连接语句中过滤掉这些内容。

此外,您可能不需要包含任何地方。包含用于预先加载,确保数据库只需要一次往返即可获得相关关系。如果您不打算访问所有这些相关属性,请不要包含它们。

我确实假设您的关系在流畅的映射文件中或使用模型上的属性正确映射。

public IEnumerable<ResourceGroup> GetAllServersByApplication(string application_name, string environment_name, string status)
{
    var query = _context.ResourceGroup
        .Include(a => a.Application)
        .Include(t => t.Type)
        .Include(e => e.ServersGroup).ThenInclude(e => e.Environment)
        .Include(s => s.ServersGroup).ThenInclude(s => s.Server)
        .Include(s => s.ServersGroup).ThenInclude(s => s.Server).ThenInclude(s => s.Status)
        .Where(a => a.Application.Name == application_name && a.ServersGroup.Any(s => s.Environment.Name == environment_name && s.Server.Status.Name == status))
        .ToList();

    return query;
}