我的 Db 上下文中有两个单独的表,TblDowntime 和 TblDowntimeReasons。他们有一个共同的 DowntimeCode 专栏,我想加入他们。我可以使用以下代码在我的 cshtml 文件中单独访问这些表:
downtimes = await _context.TblDowntimes
.OrderByDescending(Datetime => Datetime)
.Skip(pagingIndex * _pageSize)
.Take(_pageSize)
.ToListAsync();
downtimeReasons = await _context.TblDowntimeReasons
.ToListAsync();
但是,当我尝试使用 LINQ 连接这两个表时
downtimeReasonsFinal =
from d in downtimes
join r in downtimeReasons
on d.DowntimeCode equals r.DowntimeCode
orderby d.StartDowntime descending
select new
{
d.StartDowntime,
d.EndDowntime,
r.DowntimeCode,
r.DowntimeReason
}
我收到错误 CS0266告诉我我can't implicitly convert 'System.Collections.Generic.IEnumerable<<anonymous type:System.DateTime StartDowntime, System.DateTime? EndDowntime, int DowntimeCode, string DowntimeReason>>' to 'System.Collections.Generic.IEnumerable<(Website.Models.TblDowntime, Website.Models.TblDowntimeReason)>'
那么我应该如何将其转换为使其可以调用 .ToListAsync 呢?
我努力了:
- 使用表 downtimes 和 downtimeReasons 将 downtimeReasonsFinal 转换为 IEnumerable,并将其转换为列表。- 在停机时间和停机时间原因调用 .ToListAsync 之前尝试 SQL 语句
-将停机时间原因最终设置为 IAsyncEnumerable
- 同步和异步调用downtimeReasonsFinal
- 将 .ToListAsync() 与我使用 LINQ 的地方分开
- 在同一语句中使用 LINQ 和 .ToListAsync()
以及中列出的解决方案