我使用的是EntityFrameworkCore 3.1.11,并有以下查詢
。 var list = _context.Table1
.Include(i => i.ListofGroupIds)
.Where(i =>
i.ListofGroupIds.Select(x => x.GroupId).Any(m =>
SelectedIds.Where(z => z.CreatedDate <= i.SentDate).Select(y => y.Id).Contains(m)
))
);
在這里,我需要檢查SelectedIds(具有{Id,CreatedDate和其他欄位等屬性的串列)中的任何專案(Id)是否是ListOfGroupIds的一部分,根據這一點我需要獲取行。但是我得到了運行時的例外
。查詢(LINQ)運算式無法被翻譯 Entity Framework Core,要么以可被翻譯的形式重寫查詢,要么通過插入對AsEnumerable()、AsAsyncEnumerable()、ToList()或ToListAsync()的呼叫,明確切換到客戶端評估。請參閱https://go.microsoft.com/fwlink/?linkid=2101038以獲取更多資訊。
我檢查了與此相關的不同帖子,甚至嘗試了在此輸入鏈接說明
我只得到了一個解決方案,它是通過添加AsEnumerable到查詢中。但我不希望它是AsEnumerable,因為我正在處理巨大的資料,而且我不能將包含的查詢分開,因為我需要檢查一個條件(i.SentDate)在那個ANY中。
所以,如果有什么方法可以在一個單一的查詢中做到這一點,而不使AsEnumerable。
uj5u.com熱心網友回復:
假設這是你的結構(我故意忽略了你可能有的所有外鍵,這只是一個例子!)
public class Table1
{
public int Id { get; set; }
public virtual ListofGroupIds ListofGroupIds { get; set; }
}
public class ListofGroupIds ; }
{
public int GroupId { get; set; }
public DateTime SentDate { get; set; }
}
public class SelectedIds SelectedIds
{
public int Id { get; set; }
public DateTime CreatedDate { get; set; }
}
public class MyContext : DbContext
{
public DbSet< Table1> Table1 { get; set; }
public DbSet<ListofGroupIds> ListofGroupIds { get; set; }
public DbSet<SelectedIds> SelectedIds { get; set; }
你可以把你的查詢改寫成
var query = from tab1 in _context.Table1
join logi in _context.ListofGroupIds on tab1.Id equals logi.GroupId
join sids in _context.SelectedIds on logi.GroupId equals sids.Id
where sids.CreatedDate <= logi.SentDate
select new { tab1.Id, logi.GroupId, sids.CreatedDate }; //You can select any other columns within the tables joined.
或者,如果可能的話,簡單地連接需要的兩個表
。var query2 = from logi in _context.ListofGroupIds
join sids in _context.SelectedIds on logi.GroupId equals sids.Id
where sids.CreatedDate <= logi.SentDate
select new { logi.GroupId, logi.SentDate, sids.Id, sids.Credate };
或者
var query3 = _context
.ListofGroupIds.Join(_context.SelectedIds, logi => logi.GroupId, sids => sids.Id, (logi, sids) => new { logi.GroupId, logi.SentDate, sids.Id, sids.Credate })
.Where(result => result.CreatedDate <= result.SentDate)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/332720.html
標籤:
