我有点想出了我原来的问题(尽管如果有更好的方式我会喜欢听到它)。我现在遇到了Where子句
的问题where answer.TeacherID == "1234"
我需要所有7行才能返回具有TeacherID的4行。在我目前的陈述中(下面),无论我把它放在哪里它总是只返回4行......我做错了什么?
这是我更新的LINQ语句(仍然需要Where
子句)
var resultSet = (from question in db.Questions
join TeacherDetail in db.AnswerDetails on question.QuestionID
equals TeacherDetail.QuestionID into qListTeacher
from TeacherDetail in qListTeacher.DefaultIfEmpty()
join answer in db.Answers on TeacherDetail.AnswerKey
equals answer.AnswerKey into aList
from answer in aList.DefaultIfEmpty()
join LeaderDetail in db.AnswerDetails on question.QuestionID
equals LeaderDetail.QuestionID
into qListLeader
from LeaderDetail in qListLeader.DefaultIfEmpty()
select new ProfessionalObject
{
QuestionID = question.QuestionID,
IndicatorID = question.ID,
QuestionDescription = question.Description,
AnswerKey = answer.AnswerKey,
TeacherID = answer.TeacherID,
LeaderID = answer.LeaderID,
StatusKey = answer.StatusKey,
TeacherAnswerDetailKey = (answer.TeacherID != null ? TeacherDetail.AnswerDetailKey : 0),
TeacherAnswerOptionKey = (answer.TeacherID != null ? TeacherDetail.AnswerOptionKey : 0),
TeacherComment = (answer.LeaderDPSID == null ? TeacherDetail.Comment : ""),
LeaderAnswerDetailKey = (answer.LeaderID != null ? LeaderDetail.AnswerDetailKey : 0),
LeaderAnswerOptionKey = (answer.LeaderID != null ? LeaderDetail.AnswerOptionKey : 0),
LeaderComment = (answer.LeaderDPSID != null ? LeaderDetail.Comment : ""),
}).ToList();
无论是否有答案,我都有问题要打包到我的对象中。 我有一位能够回答这些问题的领导和老师
所以我想要一个返回所有问题的resultSet,我希望教师评论进入教师属性 和领导者评论进入领导者属性。
例如:
QuestionID: 1, QuestionDescription: Question 1, TeacherComment = Null, LeaderComment = "Leader Answer" , TeacherID: 12345, LeaderID: 9999
QuestionID: 2, QuestionDescription: Question 2, TeacherComment = Null, LeaderComment = "Leader Answer 2", TeacherID: 12345, LeaderID: 9999
QuestionID: 3, QuestionDescription: Question 3, TeacherComment = "Teacher Answer", LeaderComment = NULL , TeacherID: 12345, LeaderID: NULL
QuestionID: 4, QuestionDescription: Question 4, TeacherComment = "Teacher Answer 2", LeaderComment = NULL , TeacherID: 12345, LeaderID: NULL
QuestionID 5, QuestionDescription: Question 5, TeacherComment = NULL, LeaderComment = NULL , TeacherID: NULL, LeaderID: NULL
QuestionID 6, QuestionDescription: Question 6, TeacherComment = NULL, LeaderComment = NULL , TeacherID: NULL, LeaderID: NULL
QuestionID 7, QuestionDescription: Question 7, TeacherComment = NULL, LeaderComment = NULL, TeacherID: NULL, LeaderID: NULL
答案表在AnswerKey上有一个PK,还有一个TeacherID和一个LeaderID AnswerDetail表包含QuestionID和问题的评论。
我尝试添加where语句来拆分教师信息(即教师ID!= NULL和LeaderID == NULL 以及领导者信息(TeacherID!= NULL和LeaderID!= NULL)。但他们只限制了我的结果集
回答记录:
AnswerKey: 1 TeacherID: 1234 LeaderID: 9999
AnswerKey: 5 TeacherID: 1234 LeaderID: NULL
AnswerDetail记录:
AnswerKey: 1 QuestionID: 1 Comment: Leader Answer
AnswerKey: 1 QuestionID: 2 Comment: Leader Answer 2
AnswerKey: 5 Question: 3 Comment: Teacher Answer
AnswerKey: 5 Question: 4 Comment: Teacher Answer 2
问题记录:
QuestionID: 1 QuestionDescription: Question #1
QuestionID: 2 QuestionDescription: Question #2
QuestionID: 3 QuestionDescription: Question #3
QuestionID: 4 QuestionDescription: Question #4
QuestionID: 5 QuestionDescription: Question #5
QuestionID: 6 QuestionDescription: Question #6
QuestionID: 7 QuestionDescription: Question #7
更新
上面的resultSet正确返回了7个问题,而且注释在技术上是正确的。我希望实现的是,如果领导者有评论,那么我希望他们进入领导者属性,如果老师有评论,那么我希望他们进入教师属性。
领导者的逻辑是教师ID!= NULL和LeaderID!= NULL 教师的逻辑是教师ID!= NULL和LeaderID == NULL
这是我的班级:
public class ProfessionalObject
{
public int? QuestionID { get; set; }
public string IndicatorID { get; set; }
public string QuestionDescription { get; set; }
public int? AnswerKey { get; set; }
public string TeacherID { get; set; }
public string LeaderID { get; set; }
public int? StatusKey { get; set; }
public int? TeacherAnswerDetailKey { get; set; }
public int? TeacherAnswerOptionKey { get; set; }
public string TeacherComment { get; set; }
public int? LeaderAnswerDetailKey { get; set; }
public int? LeaderAnswerOptionKey { get; set; }
public string LeaderComment { get; set; }
public ProfessionalObject()
{
QuestionID = 0;
IndicatorID = "";
QuestionDescription = "";
AnswerKey = 0;
TeacherID = "";
LeaderID = "";
StatusKey = 0;
TeacherAnswerDetailKey = 0;
TeacherAnswerOptionKey = 0;
TeacherComment = "";
LeaderAnswerDetailKey = 0;
LeaderAnswerOptionKey = 0;
LeaderComment = "";
}
}
答案 0 :(得分:1)
这就是Where
的作用。从不符合条件的所有数据中过滤集合。
如果您不想过滤掉那些项目,可以删除where
答案 1 :(得分:1)
我最终使用@ShlomiBorovitz建议分解我的作品并最后加入他们:
var questionSet = ( from question in Questions select question);
var TeacherAnswer = ( from detail in AnswerDetails
join answer in Answers on detail.AnswerKey equals answer.AnswerKey
where answer.TeacherID == "12345" && answer.LeaderID == null
select new { detail, answer});
var LeaderAnswer = (from detail in AnswerDetails
join answer in Answers on detail.AnswerKey equals answer.AnswerKey
where answer.TeacherID == "12345" && answer.LeaderID != null
select new { detail, answer});
var combination = ( from q in questionSet
join tDetail in TeacherAnswer on q.QuestionID equals tDetail.detail.QuestionID into tList
from tDetail in tList.DefaultIfEmpty()
join lDetail in LeaderAnswer on q.QuestionID equals lDetail.detail.QuestionID into lList
from lDetail in lList.DefaultIfEmpty()
select new {q,tList, lList });
var resultSet = (from combo in combination
select new
{
QuestionID = (combo.q.QuestionID == null ? 0 : combo.q.QuestionID),
IndicatorID = combo.q.ID,
QuestionDescription = combo.q.Description,
TeacherAnswerKey = (combo.tList.Select(x => x.detail.AnswerKey).FirstOrDefault() == null ? 0 : combo.tList.Select(y => y.detail.AnswerKey).FirstOrDefault()),
LeaderAnswerKey = (combo.lList.Select(x => x.detail.AnswerKey).FirstOrDefault() == null ? 0 : combo.lList.Select(y => y.detail.AnswerKey).FirstOrDefault()),
TeacherID = (combo.tList.Select(y => y.answer.TeacherID).FirstOrDefault() == null ? "" : combo.tList.Select(y => y.answer.TeacherID).FirstOrDefault()),
LeaderID = (combo.lList.Select(x => x.answer.LeaderID).FirstOrDefault() == null ? "" : combo.lList.Select(y => y.answer.LeaderID).FirstOrDefault()),
TeacherStatusKey = (combo.tList.Select(x => x.answer.StatusKey).FirstOrDefault() == null ? 0 : combo.tList.Select(y => y.answer.StatusKey).FirstOrDefault()),
LeaderStatusKey = (combo.lList.Select(x => x.answer.StatusKey).FirstOrDefault() == null ? 0 : combo.lList.Select(y => y.answer.StatusKey).FirstOrDefault()),
TeacherAnswerDetailKey = (combo.tList.Select(x => x.answer.TeacherDPSID).FirstOrDefault() == null ? 0 : combo.tList.Select(y => y.detail.AnswerDetailKey).FirstOrDefault()),
TeacherAnswerOptionKey = (combo.tList.Select(x => x.answer.TeacherDPSID).FirstOrDefault() == null ? 0 : combo.tList.Select(y => y.detail.AnswerOptionKey).FirstOrDefault()),
TeacherComment = combo.tList.Select(y => y.detail.Comment).FirstOrDefault(),
LeaderAnswerDetailKey = (combo.lList.Select(x => x.answer.LeaderID).FirstOrDefault() == null ? 0 : combo.lList.Select(y => y.detail.AnswerDetailKey).FirstOrDefault()),
LeaderAnswerOptionKey = (combo.lList.Select(x => x.answer.LeaderDPSID).FirstOrDefault() == null ? 0 : combo.lList.Select(y => y.detail.AnswerOptionKey).FirstOrDefault()),
LeaderComment = combo.lList.Select(y => y.detail.Comment).FirstOrDefault(),
}).ToList();