0

次の表を考えます。

InstrumentLogs

InstrumentLogId (PK, int, not null)
InstrumentId (FK, int, not null)
LogDate (datetime, not null)
Action (string, not null)

楽器

InstrumentId (PK, int, not null)
CountyId (FK, int, not null)

CountyId (PK, int, not null)
StateFips (FK, int, not null)
Name (string, not null)

StateFips (PK, int, not null)
Name (string, not null)

Entity Framework を使用してこの SQL クエリを記述する方法を理解する必要があります。

select s.StateName, count(*) as total
from instruments as i 
join counties as c on i.CountyID = c.CountyID
join states as s on s.StateFIPS = c.StateFIPS
where i.InstrumentID in 
   (select i1.InstrumentId from InstrumentLogs as i1 where i1.action = 'review' and
    i1.logdate in (select max(logdate) from instrumentlogs as i2 where i1.instrumentid 
    =i2.InstrumentID group by i2.instrumentid))
group by s.StateName

私は次の行に沿って何かを試しました:

_context.Instruments.Include(i => i.County.State)
  .Where(i => _context.Logs.Where(l => l.Action == 'review' 
     && _context.Logs.Where(l2 => l2.InstrumentId == l.InstrumentId).Max(l2 => l2.LogDate) == l.LogDate).GroupBy(i => i.County.State.Name)
  .Select(g => new { State = g.Key.Name, Total = g.Count() });

しかし、EF はこれを好みません。プリミティブ型または列挙型のみがサポートされているというエラーが表示されます。

ご協力いただきありがとうございます。

4

1 に答える 1