U Thu

U Thu

  • NA
  • 336
  • 21.1k

Lamba experssion with Groupby Related Data Problems

Aug 2 2019 3:15 AM
I have (2) table class and (2) ViewModel
 
   public class MPInformation
    {
        public Guid ID { get; set; }
        public string Name {get;set;}
        public virtual ICollection QuestionAnswer { get; set; }
    }
    public class QuestionAnswer
    { 
        public Guid ID { get; set; }
        public Guid? PersonalID { get; set; }
        public virtual MPInformation MPInformations { get; set; }
        public string MPID {get;set;}   // equal PersonalID this properties is add GroupBy not  null state.
        public string Answers {get;set;}
    }
 
    public class MPViewModel
    {
        public Guid? ID { get; set; }
        public string Name { get; set; }
        public CounterViewModel Counter {get;set;}
       }
 
        public class CounterViewModel
         {
        public string  CounterID { get; set; }
        public int Counter { get; set; }
         }
 
IQueryable<MPViewModel> info = _context.MPInformationDB
.Select(s => new MPViewModel
  {
    ID = s.ID,
    Name = s.Name,
   Counter =  _context.QuestionAnswerDB.GroupBy(g => g.MPID)
  .Select(s => new Counter()
{
CounterID = s.Key,
Counter = s.Where(s => s.QuestionAnswerProposeID == 2).Count(),
 }).FirstOrDefault()
});
 
 The index.chtml file in
 
 @foreach (var item in Model.MPInformation)
{

@item.Name

@item.Counter.Counter

}
 
Please Help me GroupBy Problems .
 

Answers (5)