EF的连表查询Lambda表达式和linq语句(转)

Posted lizhongzhongy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了EF的连表查询Lambda表达式和linq语句(转)相关的知识,希望对你有一定的参考价值。

 
var lst = from c in db.Blogs join p in db.Posts on c.Id equals p.BlogId where p.Id==1 select c;
var lst1 = db.Blogs.Join(db.Posts.Where(p=>p.Id==1), b=> b.Id, p=> p.BlogId, (b, p) => new b);
技术图片
 public class Blog
 
    public int Id  get; set; 
    public string Title  get; set; 
 


 public class Post
 
    public int Id  get; set; 
    public string Title  get; set; 
    public int BlogId  get; set;  
 
技术图片

 

 

技术图片
        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;

//或者 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 Id = m.n.c.ID, Author = m.u.UserName, Comment =m.n.c.Content, ); return data.ToList();
技术图片

以上是关于EF的连表查询Lambda表达式和linq语句(转)的主要内容,如果未能解决你的问题,请参考以下文章

全国排名的问题(linq 的连表查询 等同于sql的left join)

SQL 语句,postgres的数据库的连表查询

点标记(lambda表达式+linq查询标记符)与linq语句(查询表达式)

c# ef框架的删除的lamda表达式怎么封装

LINQ To EF

如何将带有内连接的 sql 查询转换为 linq lambda 表达式?