C#Lambda只是请。否Linq - 查询样式。
寻找关于如何连接3个对象的策略,这些对象将输出为以下SQL(c#lambda的最终目标):
SELECT t1.*, t2.name pet_name, t3.name teacher_name
FROM user_profile t1
INNER JOIN pet_profile t2 on t1.pet_id = t2.pet_id
INNER JOIN pet_teacher t3 on t1.pet_teacher_id = t3.pet_teacher_id
user_profile 列:
pet_profile 列:
pet_teacher 列:
IQueryable<UserProfile> query = dataContext.UserProfile;
var queryTest = query
.Join(dataContext.PetProfile,
UserProfile => UserProfile.pet_id,
t2 => t2.pet_id,
(UserProfile, t2) => new { UserProfile, PetName = t2.name });
这将生成此SQL:
SELECT UserProfile.user_id,
UserProfile.user_fullname,
UserProfile.pet_id,
UserProfile.pet_teacher_id,
t2.name PetName
FROM user_profile UserProfile
INNER JOIN pet_profile t2 on UserProfile.pet_id = t2.pet_id
[Q1:]如何加入第3张表 pet_teacher
[Q2:]如何手动执行user_profile的所有字段:
var UserAndPet = query.Select(o =>
new { o.UserProfile.user_id,
o.UserProfile.user_fullname,
o.UserProfile.pet_id,
o.UserProfile.pet_teacher_id,
o.UserProfile.PetName,
o.UserProfile.TeacherName }
).ToList();
enter code here
答案 0 :(得分:1)
对于第3张表
var queryTest = query
.Join(dataContext.PetProfile,
UserProfile => UserProfile.pet_id,
t2 => t2.pet_id,
(UserProfile, t2) => new { UserProfile, PetName = t2.name })
.Join(datacontext.PetTeacher,
x=>x.UserProfile.pet_teacher_id,
y=>y.pet_teacher_id,(x,y)=>new {x.UserProfile,x.petname,x.teacherName})
对于第二个问题,如果所有列都来自同一个表
,则可以执行此操作var UserAndPet = query.Select(o =>o).ToList();