Cannot convert method group 'ToList' to non-delegate type

22,738

Solution 1

You should be calling ToList () not ToList.

Solution 2

ToList is method not a property. Should be ToList()

Solution 3

Did you mean:

    List<TutorCommission> TutorComs = newCommissions.GroupBy(g => g.Tutor).Select(s => new TutorCommission()
    {
        CommissionAmount = s.Sum(u => u.CommissionAmount) * s.Key.TutorCommissionPercentage,
        TutorNoID = s.Key.TutorNoID

    }).ToList();
Share:
22,738
Amy
Author by

Amy

Updated on July 12, 2020

Comments

  • Amy
    Amy almost 4 years

    I'm trying to write a method that generates multiple invoices. This is for a college, where clients are enrolled with tutors in a class called Enrollments. With this method, I am trying to accumulate the monthly fee of the tutors clients multiplied by their commission percentages, as tutors earn a certain commission on the lessons they give. Here is my code for this:

        public ActionResult CreateBulkCommissions()
        {
            var month = DateTime.Now.ToString("MMMM");
    
            var enrolments = db.Enrollments.ToList();
    
            var newCommissions = from enrolment in enrolments
                                 select new TutorCommission()
                                 {
                                     CommissionAmount = enrolment.MonthlyFee,
                                     CommissionMonth = month,  // string constant 
                                     CommissionStatus = "Unpaid",
                                     Tutor = new Tutor { TutorNoID = enrolment.Tutor.TutorNoID, TutorCommissionPercentage = enrolment.Tutor.TutorCommissionPercentage }
                                 };
            foreach (var newCommission in newCommissions)
            {
                List<TutorCommission> TutorComs = newCommissions.GroupBy(g => g.Tutor).Select(s => new TutorCommission
                {
                    CommissionAmount = s.Sum(u => u.CommissionAmount) * s.Key.TutorCommissionPercentage,
                    TutorNoID = s.Key.TutorNoID
    
                }).ToList;
    
                db.TutorCommission.Add(newCommission);
                db.SaveChanges();
            }
    
            return RedirectToAction("Index");
        }
    

    On the ToList line, I am receive the error that it convert convert the method group ToList to a non-delegate type. Here are my relevant model classes:

    public class Enrollment
    {
        [Key]
        [Display(Name = "Enrollment ID Number")]
        public long EnrollmentIDNumber { get; set; }
        [Display(Name = "Client ID Number")]
        public long ClientNumberID { get; set; }
        [Display(Name = "Tutor ID Number")]
        public long TutorNoID { get; set; }
        [Display(Name = "Course Name")]
        public string CourseName { get; set; }
        [Display(Name = "Lesson Time")]
        public string LessonTime { get; set; }
        [Display(Name = "Lesson Day")]
        public string LessonDay { get; set; }
        [Display(Name = "Lesson Location")]
        public string LessonLocation { get; set; }
        [Display(Name = "Lesson Type")]
        public string LessonType { get; set; }
        [Display(Name = "Lesson Level")]
        public string LessonLevel { get; set; }
        [Display(Name = "Monthly Fee")]
        public long MonthlyFee { get; set; }
    
        public virtual Client Client { get; set; }
        public virtual Tutor Tutor { get; set; }
    
    }
    
    public class TutorCommission
    {
        [Key]
        [Display(Name = "Commission ID")]
        public long CommissionID { get; set; }
        [Display(Name = "Commission Month")]
        public string CommissionMonth {get; set;}
        [Display(Name = "Commission Amount")]
        public double CommissionAmount { get; set; }
        [Display(Name = "Commission Status")]
        public string CommissionStatus { get; set; }
        [Display(Name = "Tutor ID Number")]
        public long TutorNoID { get; set; }
    
        public virtual Tutor Tutor { get; set; }
        public virtual ICollection<CommissionPayments> CommissionPayments { get; set; }
    
    }
    
    public class Tutor
    {
        [Key]
        [Display(Name = "Tutor ID Number")]
        public long TutorNoID { get; set; }
        [Required]
        [StringLength(50, ErrorMessage="First name must be less than 50 characters")]
        [Display(Name = "First Name")]
        public string TutorFirstName { get; set; }
        [StringLength(50, ErrorMessage = "Last name must be less than 50 characters")]
        [Display(Name = "Last Name")]
        public string TutorLastName { get; set; }
        [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
        [Display(Name = "Birth Date")]
        public DateTime? TutorBirthDate { get; set; }
        [Display(Name = "Cellphone Number")]
        public string TutorCellphoneNumber { get; set; }
        [Display(Name = "Home Number")]
        public string TutorHomeNumber { get; set; }
        [RegularExpression("^[a-z0-9_\\+-]+(\\.[a-z0-9_\\+-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*\\.([a-z]{2,4})$", ErrorMessage = "Not a valid email address")]
        [Display(Name = "Email Address")]
        public string TutorEmailAddress { get; set; }
        [Display(Name = "Street Address")]
        public string TutorStreetAddress { get; set; }
        [Display(Name = "Suburb")]
        public string TutorSuburb { get; set; }
        [Display(Name = "City")]
        public string TutorCity { get; set; }
        [Display(Name = "Postal Code")]
        public string TutorPostalCode { get; set; }
        [Display(Name="Full Name")]
        public string FullName
        {
            get
            {
                return TutorFirstName + " " + TutorLastName;
            }
        }
        [Display(Name="Commission Percentage")]
        [Required]
        public double TutorCommissionPercentage { get; set; }
    
        public virtual ICollection<Enrollment> Enrollments { get; set; }
        public virtual ICollection<TutorCommission> TutorCommissions { get; set; }
    
    }
    

    Thanks, Amy

  • Mike Christensen
    Mike Christensen over 12 years
    Note the () after the TutorCommission constructor too.
  • Amy
    Amy over 12 years
    Ah! Always something so simple, thanks. I'm getting a new error now though, that Validation failed for one or more entities. See 'EntityValidationErrors' property for more details. Where do I find EntityValidationErrors?
  • Adam Tuliper
    Adam Tuliper over 12 years
    EntityValidationErrors are on the exception. Download my source here and check out one way for handling them. completedevelopment.blogspot.com/2011/10/…
  • Christopher Currens
    Christopher Currens over 12 years
    The () isn't necessary after the TutorCommission constructor.
  • Mike Christensen
    Mike Christensen over 12 years
    Hmm really? I always put it in, learn something new every day I guess.
  • Christopher Currens
    Christopher Currens over 12 years
    Because you're using the object initializer, it's going to automatically use the default constructor, so the () is implied by the compiler. Of course, no harm leaving it in, either. Just a matter of preference. :)
  • Mike Christensen
    Mike Christensen over 12 years
    Yea I can definitely see how it would be redundant. I think my preference would be to leave it in just cuz I like consistency.
  • Amy
    Amy over 12 years
    Thanks. What is generally the problem with exceptions like that?