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

文章目录

        • 1、sql语句:
        • 2、linq写法:


1、sql语句:

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

2、linq写法:

1)、方法一:

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 };

你可能感兴趣的:(C#)