簡體   English   中英

實體框架核心 Lambda 表達式連接 4 個表並使用 where 進行過濾

[英]Entity Framework Core Lambda Expression join 4 tables and filter with where

我有六個表,如下所示。 我正在嘗試根據Application_nameEnvironment_nameStatus獲取數據。

狀態表:

Id, Name

申請表:

Id, Name

服務器表:

Id, ServerName, Status

環境表:

Id, Name

資源組表:

Id, Name, Application_Id, Environment_Id

服務器資源組:

Id, Server_Id, Resource_Id

我想要做的是加入所有 require 表並使用 where 來過濾數據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下看到一條紅線。 請看下圖: 在此處輸入圖片說明

有沒有更簡單的方法來編寫 lambda 查詢然后我想要做什么?

任何幫助都非常感謝。 :)

謝謝,

射線

出現錯誤是因為您現在正在返回與您的方法簽名不同的ServersGroup集合。 這是因為您添加了 Select 子句並從ResourceGroup選擇了屬性ServersGroup 過濾應該發生在Where子句中。 我更新了下面的代碼,並將Select中的過濾器移動到Where ,也將包含的 where 更改為Any作為 Where 期望布爾子句。

如果您打算只在您的ResourceGroup包含某些ServersGroup ,那么您應該在連接或外部連接語句中過濾掉那些。

此外,您可能不需要到處都包含。 包含用於急切加載,以確保數據庫只需要一次往返即可獲取關聯關系。 如果您不打算訪問所有這些相關屬性,則不要包含它們。

我確實假設您的關系在流暢的映射文件中或使用模型上的屬性正確映射。

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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM