Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# LINQ左连接-Where语句问题_C#_Linq - Fatal编程技术网

C# LINQ左连接-Where语句问题

C# LINQ左连接-Where语句问题,c#,linq,C#,Linq,我已经找到了我最初的问题(尽管如果有更好的方法,我很乐意听到)。我现在对Where条款有意见 where answer.TeacherID == "1234" 我需要全部7行返回,而不仅仅是包含TeacherID的4行。在我当前的语句(下面)中,无论我把它放在哪里,它总是只返回4行。。。我做错了什么 这是我更新的LINQ语句(仍然需要Where子句) 我有一些问题要打包到我的对象中,不管是否有答案。 我有一位领导和一位老师有能力回答这些问题 所以我想要一个返回所有问题的结果集,并且我希望教师的

我已经找到了我最初的问题(尽管如果有更好的方法,我很乐意听到)。我现在对Where条款有意见

where answer.TeacherID == "1234"
我需要全部7行返回,而不仅仅是包含TeacherID的4行。在我当前的语句(下面)中,无论我把它放在哪里,它总是只返回4行。。。我做错了什么

这是我更新的LINQ语句(仍然需要
Where
子句)

我有一些问题要打包到我的对象中,不管是否有答案。 我有一位领导和一位老师有能力回答这些问题

所以我想要一个返回所有问题的结果集,并且我希望教师的评论进入教师属性 和Leader注释进入Leader属性。

例如:

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: 1 TeacherID: 1234 LeaderID: 9999

AnswerKey: 5 TeacherID: 1234 LeaderID: NULL
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
答题台的答题键上有一个PK,还有一个教师ID和一个领导者ID AnswerDetail表包含问题ID和对问题的注释

我尝试添加where语句来拆分教师信息(即where TeacherID!=NULL和LeaderID==NULL) 以及领导者信息(TeacherID!=NULL和LeaderID!=NULL)。但他们只是限制了我的结果集

回答记录:

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: 1 TeacherID: 1234 LeaderID: 9999

AnswerKey: 5 TeacherID: 1234 LeaderID: NULL
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
回答详细记录:

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: 1 TeacherID: 1234 LeaderID: 9999

AnswerKey: 5 TeacherID: 1234 LeaderID: NULL
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
问题记录:

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: 1 TeacherID: 1234 LeaderID: 9999

AnswerKey: 5 TeacherID: 1234 LeaderID: NULL
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
更新:

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: 1 TeacherID: 1234 LeaderID: 9999

AnswerKey: 5 TeacherID: 1234 LeaderID: NULL
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
上面的结果集正确地返回了7个问题,并且注释在技术上是正确的。我希望实现的是,如果领导者有注释,那么我希望他们进入领导者属性,如果老师有注释,那么我希望他们进入教师属性

如果TeacherID!=NULL和LeaderID!=NULL,则为Leader的逻辑 教师的逻辑是TeacherID!=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 = "";
    }
}

这就是
的作用所在。从所有不符合条件的数据中筛选集合


如果您不想过滤掉这些项目,您可以删除where,我最后使用@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();

我需要更多的左连接类型。我知道where子句将过滤掉行,但我需要没有注释的问题也显示在我的结果中。在我的问题中,您将看到我正在寻找的结果集的“示例”。我会给您一些建议,因为这样大的查询很难调试和理解(特别是如果你和我一样在打电话的话)。创建多个简单的查询,每个查询都可以从某个来源检索特定数据,然后将它们组合成合适的查询(当我靠近计算机时,我会更仔细地查看你的查询)事实上,你的分手评论让我走上了正确的道路。你还需要帮助吗?你实际上不需要所有这些括号(除了最后一个查询,因为调用了
ToList
):P@ShlomiBorovitz-你说的“括号”是什么意思?再次感谢你的帮助。这是最不重要的事情,但是,我指的是所有这些
()
围绕您的查询。这就是我添加“:P”的原因