public List<CommentsViewModel> GetCommentsByPid(int pid, int uid) { var query = from a in db.Photos join b in db.Comments on a.PID equals b.PID join c in db.Users on b.UID equals c.UID where a.PID == pid select new CommentsViewModel { Id = b.ID, Author = c.UserName, Comment = b.Content, UserAvatar = c.HeadPic, CanDelete = uid == b.UID ? true : false, CanReplay = true, TempDate = b.CreateTime, ParentId = b.ParentId }; var ret = query.ToList(); return ret; }
public class CommentsViewModel { public int Id { get; set; } public string Author { get; set; } public string Comment { get; set; } public int? ParentId { get; set; } public string UserAvatar { get; set; } public bool CanDelete { get; set; } public bool CanReplay { get; set; } public string Date { get; set; } public DateTime TempDate { get; set; } }
var data= db.Photos. Join(db.Comments, p => p.PID, c> c.PID, (p,c) => new { p, c }). Join(db.Users, n => n.c.UID, u => u.UID, (n, u) => new { n, u }) .Where(m => m.n.p.PID == pid) .Select (m => new CommentsViewModel { //something });
好像有一点错误
我知道哪里错了,豆子全给你了
大神,我想知道//something里面怎么写
@天涯过者: 根据最后一次new 的 点出来就可以
@Yu: 谢谢了
听不懂.表达式是一个方法,弄不了这些的.
你说的是改为方法调用的形式吗?
我说的是想把 var query = from a in db.Photos join b in db.Comments on a.PID equals b.PID join c in db.Users on b.UID equals c.UID where a.PID == pid select new CommentsViewModel { Id = b.ID, Author = c.UserName, Comment = b.Content, UserAvatar = c.HeadPic, CanDelete = uid == b.UID ? true : false, CanReplay = true, TempDate = b.CreateTime, ParentId = b.ParentId };这个语句改写成 db.Photos.Join(db.Comments, p => p.PID, c => c.PID, (p, c) => new { Id = p.PID });的这种形式的