C# LINQ Join两个表连接,关联多个条件的写法


1、sql语句:

select * from Users u join Teachers t 
on u.UserID==t.TeacherID and u.Name=t.Name

2、linq写法:

1)、方法一:
将多个字段的比较,new 一个对象进行比较;
注意:对象的属性名要保持一致;

var userInfos = from u in db.Users join t in db.Teachers 
                on new { u.UserID , u.Name } equals new {  UserID = t.TeacherID,t.Name }
                select new { ID=u.UserID, t.Name };

2)、方法二:

var userInfos = from u in db.Users
                from t in db.Teachers
                where u.UserID == t.TeacherID && u.Name == t.Name
                select new { ID=u.UserID, t.Name };

posted @ 2019-11-16 17:37  willingtolove  阅读(...)  评论(... 编辑 收藏