如何使用Linq从单独的表中填充一个视图
本文关键字:视图 一个 填充 Linq 何使用 单独 | 更新日期: 2023-09-27 18:24:41
我有表格:教师和学生。
我们有一个特殊项目表,教师和学生被分配在一起的项目中工作
SpecProjID, SpecProjName, isTeacher , teacherID , isStudent, studentID
我们必须用值填充每个特殊项目的视图:名字,姓氏,电子邮件。ToList()
我们需要从两个独立的表中获取信息,逻辑如下:
If isTeacher then get FirstName from Teacher table
If isStudent then get FirstName from Student table
并填充View.ToList()
如何使用Linq从单独的表中填充一个视图?
这就是您想要的吗?
var view = Teachers.Select(
t => new { t.FirstName, t.LastName, t.Email } ).
Union( Students.Select(
t => new { t.FirstName, t.LastName, t.Email }) ).ToList();
@Phil谢谢,它成功了。。在你的帮助下,我能够使整个代码正常工作。联盟很圆滑:)
var rosterTeacher = ctx.SpecialProject
.Where(sp => sp.caseID == cID && sp.isTeacher)
.Join(ctx.Teachers, m => m.teacherID, p => p.teacherID,
(sp, m) => new SpecProjRosterView
{
RosterEmail = m.memEMail,
RosterName = (m.memLastName + ", " + m.memFirstName),
RosterVerified = sp.caseRosterIsVerified
}).ToList();
var rosterStudents = ctx.SpecialProject
.Where(c => c.caseID == cID && c.isStudents)
.Join(ctx.CaseStudents, u => u.studentID, p => p.studentID,
(sp, m) => new SpecProjRosterView
{
RosterEmail = m.caseStudentsEMail,
RosterName = (m.caseStudentsLastName + ", " + m.caseStudentsFirstName),
RosterVerified = sp.caseRosterIsVerified
}).ToList();
return rosterTeacher.Union(rosterStudents).ToList();