cor*_*010 1 c# entity-framework fluent
我有表 Table1、Table2、Table3 等。为了处理审核,我使用包含以下字段 Id、Username、ChangeTime、ChangeType、TableName 和 TableRecordId 的 Audit 表。
要获取 Table1 中与 Table1.Id = Audit.TableRecordId 上的审核连接的所有记录,其中用户名是“jdoe”,例如,我有以下内容:
var results = db.Table1s
.Join(db.Audits.Where(a => a.Username == "jdoe"),
t => t.Id,
a => a.RecordId,
(t, a) => new { Table1 = t, Audit = a });
Run Code Online (Sandbox Code Playgroud)
但是当我运行它时,我收到以下错误:
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[<>f__AnonymousType1`2[SampleApp.Models.Table1,AuditLibraryContext.Models.Audit]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[AuditTrackerSample.Models.Table1]'.
Run Code Online (Sandbox Code Playgroud)
让它工作我最终得到了以下代码:
var results = db.Table1s
.Join(db.Audits.Where(a => a.Username == "jdoe"),
t => t.Id,
a => a.RecordId,
(t, a) => t);
Run Code Online (Sandbox Code Playgroud)
在提出一般性重复问题之前,请先进行彻底搜索。
var results = db.Table1s
.Join(db.Audits.Where(a => a.Username == "jdoe"),
t => t.Id,
a => a.TableRecordId,
(t, a) => new { Table1 = t, Audit = a });
var results = from t in db.Table1s
join a in db.Audits
on t.Id equals a.TableRecordId
where a.Username == "jdoe"
select new { Table1 = t, Audit = a};
Run Code Online (Sandbox Code Playgroud)