一个数据库条目中的总金额- MVC 3
本文关键字:金额 MVC 一个 数据库 | 更新日期: 2023-09-27 18:11:07
我为一所大学的系统编写了一个为导师生成佣金单的方法。导师每月收取一定的费用,让特定的学生注册,并从他们提供的课程中赚取佣金。通过这种方法,我试图计算所有导师注册的总金额(即如果他们有3个学生,每个学生每月注册R460,总金额将是1380乘以他们的佣金百分比。)因此,我希望为数据库中的每个导师生成一张佣金单(TutorCommission实体中的一个数据库条目)。下面是它的代码:
public ActionResult CreateBulkCommissions()
{
{
var month = DateTime.Now.ToString("MMMM");
var enrolments = db.Enrollments.ToList();
var tutor = db.Tutors.ToList();
IEnumerable<TutorCommission> tutorsCommissionsAlt = enrolments // Take the enrollments
.GroupJoin( // This will group enrollments by the tutor
tutor, // Join with the tutor's commission percentages.
e => e.TutorNoID, // Use tutorNoID for left Key
tcp => tcp.TutorNoID, // ... and right key
(e, tcp) => new TutorCommission // Create entry which is the tutor and his total commission
{
TutorNoID = e.TutorNoID,
CommissionAmount = (long)tcp.Sum(c => c.TutorCommissionPercentage * e.MonthlyFee),
CommissionMonth = month, // string constant
CommissionStatus = "Unpaid",
});
foreach (var com in tutorsCommissionsAlt)
{
db.TutorCommission.Add(com);
db.SaveChanges();
}
return RedirectToAction("Index");
}
}
目前,这段代码为每个登记生成一个新的数据库条目(因此,如果他们有20个登记,它将为当月生成20个数据库条目)。我可以对这段代码做什么更改,以便将每个导师的总金额放在一个数据库条目中?下面是相关的类:
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; }
谢谢,艾米
看起来您的集合指定的方式是错误的-您不应该加入一个单个导师的注册列表吗?:
IEnumerable<TutorCommission> tutorsCommissionsAlt = enrolments.GroupJoin(tutor ...
应:IEnumerable<TutorCommission> tutorsCommissionsAlt = tutor.GroupJoin(enrolments...
我把这段代码放在一起,它似乎检查出来了:
var month = DateTime.Now.ToString("MMMM");
var enrolments = new List<Enrollment>();
enrolments.Add(new Enrollment { TutorNoID = 1, MonthlyFee = 1500 });
enrolments.Add(new Enrollment { TutorNoID = 1, MonthlyFee = 4500 });
var tutor = new List<Tutor>();
tutor.Add(new Tutor { TutorNoID = 1, TutorCommissionPercentage = 0.5 });
IEnumerable<TutorCommission> tutorsCommissionsAlt = tutor.GroupJoin(enrolments,
tut => tut.TutorNoID,
enr => enr.TutorNoID,
(tut, enr) => new TutorCommission // Create entry which is the tutor and his total commission
{
TutorNoID = tut.TutorNoID,
CommissionAmount = (long)enr.Sum(c => c.MonthlyFee * tut.TutorCommissionPercentage),
CommissionMonth = month, // string constant
CommissionStatus = "Unpaid",
});
foreach (var com in tutorsCommissionsAlt)
{
Console.WriteLine(com.CommissionAmount);
}
// Output: 3000