Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

get related data using Ef core 3.1 & view model

$
0
0

hello i trying to get related data from 3 db tables so i will get at the results a list of Quetta with a list of QuoteQuestion with a single answer .for each QuoteQuestion 

my models look like this : 

 public class QuettaReq
    {
        public QuettaReq()
        {
            QuettaOffer = new List<QuettaOffer>();
            QuoteQuestions = new List<QuoteQuestion>();

        }

        [Key] public int Id { get; set; }
 public Category Category { get; set; }

        public int CatId { get; set; }
 public virtual List<QuettaOffer> QuettaOffer { get; set; }
        public virtual List<QuoteQuestion> QuoteQuestions { get; set; }
// Parm removed for clear forum question 

 public class QuoteQuestion
    {
        [Key]
        public int QuoteQuestionId { get; set; }

        public int QuoteId { get; set; }
        public virtual QuettaReq QuettaReq { get; set; }
    public virtual IList<Answers> Answers { get; set; }
//Parm  removed for clear forum question 

 public class Answers
    {
        public int AnswersId { get; set; }
        public int QuoteQuestionId { get; set; }
        public string Answer { get; set; }
        public int QuoteId { get; set; }
//Parm removed for clear forum question 
    }

My view model look like this :

    public class ReqestWithQA
    {
        [Key] public int Id { get; set; }
        public Category Category { get; set; }
        public int CatId { get; set; }
       public string CatName { get; set; }
       public virtual IList<QAViewModel> QAViewModel { get; set; }
        public int QuettaReqId { get; set; }
        public string Question { get; set; }
        //Parm  removed for clear forum question 
    }
public class QAViewModel
{
public int QuoteQuestionId { get; set; }
public int Id { get; set; }
public string Question { get; set; }
public string SiteUserId { get; set; }
public virtual IList<Answers> Answers { get; set; }
}

in my view i get the data 
 

@model IEnumerable<MyProject.ViewModels.ReqestWithQA>

   @foreach (var item in Model)
                    {

//parm
  @foreach (var qestion in item.QAViewModel)
                                        {

//Parm
  if (qestion.Answers != null)
                                            {
                                                foreach (var ansewr in qestion.Answers)
                                                {
//Parm


}

}
  1. i know i can use partail view but this is a project from core 2.2 that i need to update to 3.1 . and the view  ui etc. are already  set
  2. i hope my question is clear 

this is the query i have used in EF 2.2 

 List<int> inList =
                    await _context.supSelections.Where(d => d.SupplayerId == getCategoryWsupId.SupplayerId).Select(a => a.QuettaId)
                          .ToListAsync();

                IQueryable<ReqestWithQA> fitReqests =
                    _context.Quetta
                    .Where(c => c.Category.CatName == getCategoryWsupId.CatName&& c.OfferDate < DateTime.Now)
                  .Where(bb => !inList.Contains(bb.Id))

                   .GroupJoin(_context.quoteQuestions.OrderBy(a => a.QuoteId),
                                quetta => quetta.Id,
                                    qestion => qestion.QuoteId, (quetta, joinQestionQuetta) => new ReqestWithQA
                                    {
                                        ReqText = quetta.ReqText,
                                        Qentity = quetta.Qentity,
                                        CatId = quetta.CatId,
                                        CatName = quetta.Category.CatName,
                                        District = quetta.District,
                                        ApplicationDate = quetta.ApplicationDate,
                                        DeliveryDate = quetta.DeliveryDate,
                                        OfferDate = quetta.OfferDate,
                                        TecnicalDetails = quetta.TecnicalDetails,
                                        Bedget = quetta.Bedget,
                                        Id = quetta.Id,
                                        QAViewModel =
                                            joinQestionQuetta
                                                .Select(q => new QAViewModel
                                                {
                                                    Question = q.Question,
                                                    Answers = q.Answers,
                                                    Id = q.QuoteId,
                                                    QuoteQuestionId = q.QuoteQuestionId
                                                }).ToList()
                                    });

                return View(fitReqests);


And the error im getting :

<div class="titleerror">validOperationException: Processing of the LINQ expression 'DbSet<QuettaReq>
.Where(c => c.Category.CatName == __getCategoryWsupId_CatName_0 && c.OfferDate < DateTime.Now)
.Where(bb => !(__inList_1.Contains(bb.Id)))
.GroupJoin(
outer: DbSet<QuoteQuestion>
.OrderBy(a => a.QuoteId),
inner: quetta => quetta.Id,
outerKeySelector: qestion => qestion.QuoteId,
innerKeySelector: (quetta, joinQestionQuetta) => new ReqestWithQA{
ReqText = quetta.ReqText,
Qentity = quetta.Qentity,
CatId = quetta.CatId,
CatName = quetta.Category.CatName,
District = quetta.District,
ApplicationDate = quetta.ApplicationDate,
DeliveryDate = quetta.DeliveryDate,
OfferDate = quetta.OfferDate,
TecnicalDetails = quetta.TecnicalDetails,
Bedget = quetta.Bedget,
Id = quetta.Id,
QAViewModel = joinQestionQuetta
.Select(q => new QAViewModel{
Question = q.Question,
Answers = q.Answers,
Id = q.QuoteId,
QuoteQuestionId = q.QuoteQuestionId
}
)
.ToList()
}
)' by 'NavigationExpandingExpressionVisitor' failed. This may indicate either a bug or a limitation in EF Core. See https://go.microsoft.com/fwlink/?linkid=2101433 for more detailed information.</div>

Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)




Thanks in advance 


Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>