C# 如何使用LINQ连接多个选择?

C# 如何使用LINQ连接多个选择?,c#,linq,select,C#,Linq,Select,我有一个主选择,如下所示: protected IQueryable<Answers> GetActualAnswers<TAns>(DateTime? start, DateTime? end, long? statusId) where TAns: AnswersBase { _contex.Set<TAns>.Where(x => x.Type == VoteType.Good) .Select(vv => new Answers

我有一个主选择,如下所示:

protected IQueryable<Answers> GetActualAnswers<TAns>(DateTime? start, DateTime? end, long? statusId) where TAns: AnswersBase
{
_contex.Set<TAns>.Where(x => x.Type == VoteType.Good)
.Select(vv => new Answers
        {
             CreatedAt = vv.CreatedAt,
             StatusId = vv.StatusId,
             Type = vv.Type ,
             AnswerInGuideStatusId = vv.AnswerInGuideStatusId
        }
}
受保护的IQueryable GetActualsWers(DateTime?开始、DateTime?结束、long?状态ID),其中TAns:AnswersBase
{
_contex.Set.Where(x=>x.Type==VoteType.Good)
.选择(vv=>新答案
{
CreatedAt=vv.CreatedAt,
StatusId=vv.StatusId,
类型=vv.类型,
AnswerInGuideStatusId=vv.AnswerInGuideStatusId
}
}
我在两个简单的查询中使用此方法:

 var result1 = GetActualAnswers<JournalAnswers>(start, end, statusId)
    .Select(j => new UnitedAnswers
    {
         Question = j.Question,
    }

    var result2 = GetActualAnswers<BoAnswers>(start, end, statusId)
    .Select(b => new UnitedAnswers
    {
         Prospects = b.Prospects ,
    }

var mainResult = result1.Concat(result2);
var result1=GetActualAnswers(开始、结束、状态ID)
.选择(j=>new UnitedAnswers
{
问题,
}
var result2=GetActualAnswers(开始、结束、状态ID)
.选择(b=>new UnitedAnswers
{
前景,
}
var mainResult=result1.Concat(result2);
我发现错误:

Sql = Sql = '((System.Data.Entity.Infrastructure.DbQuery<UnitedAnswers>)result1).Sql' threw an exception of type 'System.NotSupportedException'
Sql = Sql = '((System.Data.Entity.Infrastructure.DbQuery<UnitedAnswers>)result2).Sql' threw an exception of type 'System.NotSupportedException'
Sql = Sql = '((System.Data.Entity.Infrastructure.DbQuery<UnitedAnswers>)mainResult).Sql' threw an exception of type 'System.NotSupportedException'
Sql=Sql='((System.Data.Entity.Infrastructure.DbQuery)result1)。Sql'引发了类型为'System.NotSupportedException'的异常
Sql=Sql='((System.Data.Entity.Infrastructure.DbQuery)result2)。Sql'引发了“System.NotSupportedException”类型的异常
Sql=Sql='((System.Data.Entity.Infrastructure.DbQuery)mainResult)。Sql'引发了“System.NotSupportedException”类型的异常

是否可以使用多个选项?可能有人可以通过此查询提供建议?

首先,我想知道属性
j.Question
b.Prospects
的位置,而在方法
GetActualAnswers
中,您没有得到上面两个属性的值

其次,在方法
GetActualAnswers
中,您返回的是
IQueryable
,因此您应该检查
empty
而不是
null

那么你的案子可能是这样的

var mainResult = Enumerable.Concat(
resut1 ?? Enumerable.Empty<UnitedAnswers>(),
resut2 ?? Enumerable.Empty<UnitedAnswers>()
以下链接对您很有用


  • 单独执行时,这两个查询result1和result2有效吗?"您正在使用的查询中的4!类中可能存在不受支持的情况。尤其是Answers类没有问题或潜在客户属性集,您如何复制它们?为什么您首先复制到Answers而不是UnitedAnswers?为什么不立即复制到UnitedAnswers,会导致较少的复杂性。您认为
    j.Questio在哪里n
    b.潜在客户将来自?这能回答你的问题吗?如果你需要帮助,请告诉我。@t不幸的是,这个答案对我没有帮助。我改变了概念以绕过这个问题。谢谢你的帮助。
    我改变了概念以绕过这个问题
    ,你是说我的代码有效,但你改变了你的密码解决方案而不是
    concat multiple selects
    ?您的代码没有帮助。字段有问题。它应该是简单的dto对象。所以我使用Automapper在执行它们之前生成ef查询。
      var mainResult = Enumerable.Concat(
        result1.AsEnumerable(),
        result2.AsEnumerable());