0

I have main select which looks like this:

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
        }
}

I'm using this method in two simple queries:

 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);

I get errors:

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'

Is it possible to use several Selects? May be someone can give advice with this query?

TAD
  • 3
  • 3
  • Do the two queries result1 and result2 work, when executed separatly ? "Not supported might be something in the 4! classes in your queries you are using. Especially the Answers class does not have a Question or Prospects properties set, how can you copy them ? And why you first copy to Answers, than to UnitedAnswers ? Why not to UnitedAnswers right away, would cause less complication. – Holger Dec 27 '19 at 11:16
  • Where do you think `j.Question` and `b.Prospects` will come from? – NetMage Dec 27 '19 at 21:37

1 Answers1

0

Firstly, I was wondering where the property j.Question and b.Prospects you get, While in the method GetActualAnswers you did not get the value of 2 properties above.

Secondly, At the method GetActualAnswers you were returning IQueryable, so you should check empty instead of null value

Then your case might look like this

var mainResult = Enumerable.Concat(
resut1 ?? Enumerable.Empty<UnitedAnswers>(),
resut2 ?? Enumerable.Empty<UnitedAnswers>()

or

  var mainResult = Enumerable.Concat(
    result1.AsEnumerable(),
    result2.AsEnumerable());

The following links are useful for you.

  1. How to merge two IQueryable lists
  2. Enumerable.AsEnumerable
Nguyễn Văn Phong
  • 13,506
  • 17
  • 39
  • 56
  • Does this answer your question? Please let me know if you need any help. @TAD – Nguyễn Văn Phong Dec 30 '19 at 06:17
  • Unfortunately this answer didn't help me. I had changed conception to bypass this problem. Thank you for helping. – TAD Jan 08 '20 at 09:31
  • `I had changed conception to bypass this problem`, You mean my code worked but you changed your solution instead of `concat multiple selects`? – Nguyễn Văn Phong Jan 08 '20 at 09:33
  • Your code doesn't help. There is problem with field. It should be simple dto object. So I use Automapper to generate ef query before execute them. – TAD Jan 08 '20 at 11:45