2011-10-11 70 views
3

我試圖編寫一個方法來生成多個發票。這是一個大學,在一個名爲「註冊」的課程中,客戶將與導師一起報名。用這種方法,我試圖累積導師客戶的月費加上他們的佣金百分比,因爲導師在他們提供的課程上賺取一定的佣金。這裏是我的這段代碼:無法將方法組'ToList'轉換爲非委託類型

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

在ToList行,我收到其轉換方法組ToList轉換到非委託類型的錯誤。以下是我的相關模型類:

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

} 

感謝, 艾米

回答

18

你應該調用ToList ()沒有ToList。

+0

啊!總是這麼簡單,謝謝。但是,現在我收到了一個新的錯誤,即一個或多個實體的驗證失敗。有關更多詳細信息,請參閱「EntityValidationErrors」屬性。我在哪裏可以找到EntityValidationErrors? – Amy

+0

EntityValidationErrors出現異常。在這裏下載我的源代碼並查看處理它們的一種方法。 http://completedevelopment.blogspot.com/2011/10/codecamp-nyc-entity-framework-mvc-code.html –

+0

謝謝。這種異常通常會遇到什麼問題? – Amy

1

你的意思是:

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(); 
+0

注意TutorCommission構造函數之後的()。 –

+1

TutorCommission構造函數後面的()不是必需的。 –

+0

真的嗎?我總是把它放進去,我猜每天都會學到新的東西。 –

2

ToList是方法不是屬性。應該是ToList()

-1
protected void Button3_Click(object sender, EventArgs e) 
    { 
     SqlDataReader dr = null; 
     SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=shoping;User ID=sa;Password=yamini"); 
     con.Open(); 
     SqlCommand cmd = new SqlCommand("Select email from usertd where @email='" + TextBox1.Text + "' ", con); 
     cmd.Parameters.AddWithValue("@email", TextBox1.Text); 
     dr = cmd.ExecuteReader; 
     if (dr != null && dr.HasRows) 
     { 
      TextBox2.Text = "abc"; 
     } 
     else 
     { 
      TextBox2.Text = "hdc"; 
     } 

    } 
相關問題