簡體   English   中英

在 Entity Framework Core 中進行二級過濾

[英]Filter on second level in Entity Framework Core

我有一個台式工作站和評論。 當我詢問工作站列表時,我只想包括工作站的最新評論。 由於數據和性能的減少。 但我不知道如何寫入一個查詢。 我到底應該怎么做?

var workstations = this.context.TWorkstation.Include(x => x.TComments).AsQueryable();
workstations = workstations.SelectMany(x => x.TComments.Max(y => y.CreationDate)).ToList();

目前,回復看起來像這樣,包含評論的 id 1 和 id2。 但它應該只包含 id 2

"workstation": "workstaionName",
"alias": "alias",
"dns": "ip",
"macAddress": "FF-FF-FF-FF-FF-F",
"comments": [
    {
        "id": 1,
        "text": "test\n",
        "creationDate": "2020-11-16T17:41:40.226617",
        "modificationDate": "0001-01-01T00:00:00",
        "creator": "xxx"
    },
    {
        "id": 2,
        "text": "test2",
        "creationDate": "2020-11-17T17:41:40.226617",
        "modificationDate": "0001-01-01T00:00:00",
        "creator": "xxx"
    }
],
"creator": "xxx"

在 EF Core 5 中,您可以執行以下操作:

var workstations = this.context.TWorkstation
   .Include(x => x.TComments.OrderByDescending(c => c.CreationDate).Take(1))
   .ToList();

暫無
暫無

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

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