实体框架加入3个表我想加入三张桌子,但我无法理解这个方法......我完成了加入2个表 var entryPoint = dbContext.tbl_EntryPoint .Join(dbContext.tbl_Entry,
c => c.EID,
cm => cm.EID,
(c, cm) => new
{
UID = cm.OwnerUID,
TID = cm.TID,
EID = c.EID,
}).
Where(a => a.UID == user.UID).Take(10);我想在TID PK中包含tbl_Title表并获取Title字段。非常感谢
3 回答
慕神8447489
TA贡献1780条经验 获得超1个赞
我认为使用基于语法的查询会更容易:
var entryPoint = (from ep in dbContext.tbl_EntryPoint join e in dbContext.tbl_Entry on ep.EID equals e.EID join t in dbContext.tbl_Title on e.TID equals t.TID where e.OwnerID == user.UID select new { UID = e.OwnerID, TID = e.TID, Title = t.Title, EID = e.EID }).Take(10);
你应该添加orderby
子句,以确保Top(10)
返回正确的前十项。
红颜莎娜
TA贡献1842条经验 获得超12个赞
这是未经测试的,但我相信语法应该适用于lambda查询。当您使用此语法连接更多表时,您必须深入查看新对象以获得要操作的值。
var fullEntries = dbContext.tbl_EntryPoint .Join( dbContext.tbl_Entry, entryPoint => entryPoint.EID, entry => entry.EID, (entryPoint, entry) => new { entryPoint, entry } ) .Join( dbContext.tbl_Title, combinedEntry => combinedEntry.entry.TID, title => title.TID, (combinedEntry, title) => new { UID = combinedEntry.entry.OwnerUID, TID = combinedEntry.entry.TID, EID = combinedEntry.entryPoint.EID, Title = title.Title } ) .Where(fullEntry => fullEntry.UID == user.UID) .Take(10);
绝地无双
TA贡献1946条经验 获得超4个赞
var entryPoint = (from ep in dbContext.tbl_EntryPoint join e in dbContext.tbl_Entry on ep.EID equals e.EID join t in dbContext.tbl_Title on e.TID equals t.TID where e.OwnerID == user.UID select new { UID = e.OwnerID, TID = e.TID, Title = t.Title, EID = e.EID }).Take(10);
- 3 回答
- 0 关注
- 333 浏览
添加回答
举报
0/150
提交
取消