首页新闻找找看学习计划

求大神帮我把这个linq语句转换成lambda的表达式

0
悬赏园豆:30 [已解决问题] 解决于 2015-07-06 11:00
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; }
    }
天涯过者的主页 天涯过者 | 初学一级 | 园豆:177
提问于:2015-07-06 10:22
< >
分享
最佳答案
1
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
        });
收获园豆:30
Yu | 专家六级 |园豆:12944 | 2015-07-06 10:47

天涯过者 | 园豆:177 (初学一级) | 2015-07-06 10:53

好像有一点错误

天涯过者 | 园豆:177 (初学一级) | 2015-07-06 10:53

我知道哪里错了,豆子全给你了

天涯过者 | 园豆:177 (初学一级) | 2015-07-06 10:59

大神,我想知道//something里面怎么写

天涯过者 | 园豆:177 (初学一级) | 2015-07-06 11:11

@天涯过者: 根据最后一次new 的 点出来就可以

Yu | 园豆:12944 (专家六级) | 2015-07-06 11:21

@Yu: 谢谢了

天涯过者 | 园豆:177 (初学一级) | 2015-07-06 11:22
其他回答(1)
0

听不懂.表达式是一个方法,弄不了这些的.

你说的是改为方法调用的形式吗?

吴瑞祥 | 园豆:28675 (高人七级) | 2015-07-06 10:46

我说的是想把 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 });的这种形式的

支持(0) 反对(0) 天涯过者 | 园豆:177 (初学一级) | 2015-07-06 10:49
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册