GroupJoin在实体框架中引发错误。加入工作正常

时间:2019-10-22 19:50:17

标签: c# entity-framework lambda

var conversationQABySubjectId = 
    _repoWrapper.ConversationQuestionAnswer
                .FindAll()
                .Where(a => a.SubjectId == subjectId)
                .GroupJoin(_repoWrapper.ContactInformation.FindAll(),
                           c => c.ResponderContactInformationIdentifier,
                           ci => ci.ContactInformationIdentifier,
                           (c, ci) => new { c, ci })
                .Select(m => new Tuple<Domain.TC_Context.ConversationQuestionAnswer,
                                 Domain.TC_Context.ContactInformation>(m.c, m.ci));

m.ci抛出此错误,如下所示。当我使用Join时,此方法可以正常工作。我正在使用GroupJoin,因为我想退出Join ContactInformation表。

  

参数2:无法从“ System.Collections.Generic.IEnumerable”转换为“ TrainingCenter.Domain.TC_Context.ContactInformation”

添加了SelectManyDefaultIfEmpty,但是没有起作用。下面是代码

var conversationQABySubjectId = _repoWrapper.ConversationQuestionAnswer.FindAll()
                                    .Where(a => a.SubjectId == subjectId)
                                    .GroupJoin(_repoWrapper.ContactInformation.FindAll(),
                                    c => c.ResponderContactInformationIdentifier,
                                    ci => ci.ContactInformationIdentifier,
                                    (c, ci) => new { c, ci })
                                    .SelectMany(cci => cci.ci.DefaultIfEmpty(), (c, ci) => new { c, ci })
                                    .Select(m => new Tuple<Domain.TC_Context.ConversationQuestionAnswer,
                                                            Domain.TC_Context.ContactInformation>(m.c, m.ci));

1 个答案:

答案 0 :(得分:0)

以下是我如何解决此问题。

var conversationQABySubjectId = _repoWrapper.ConversationQuestionAnswer.FindAll()
                                        .Where(a => a.SubjectId == subjectId)
                                        .GroupJoin(_repoWrapper.ContactInformation.FindAll(),
                                        c => c.ResponderContactInformationIdentifier,
                                        ci => ci.ContactInformationIdentifier,
                                        (c, ci) => new { c, ci })
                                        .SelectMany(m => m.ci.DefaultIfEmpty(), (m,n) => new Tuple<Domain.TC_Context.ConversationQuestionAnswer,
                                                               Domain.TC_Context.ContactInformation>(m.c, n));