The second error was because we were working with a list and not the entity direct
now when we ae making a commission obect, we are adding the tutor object, and then adding the
TutorCommissionPercentageobject to the Tutor.
i have a question, why is the TutorCommissionPercentage a child object, should
CommissionPercentage simply be a property of Tutor?
Are there more then one tutor comissions?
I think you will still get an error.
to fix, you can either make
CommissionPercentage a property of Tutor and not a collection of child objects
or you can change this line enrolment.Tutor.TutorCommissionPercentage.CommissionPercentage
to enrolment.Tutor.TutorCommissionPercentage.firstordefault.CommissionPercentage
Okay I've made TutorCommissionPercentage a property of Tutor. The reason I had it alone was because I wanted to keep it confidential but I can still do that by making it a property.
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 = new TutorCommissionPercentage { CommissionPercentage = 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");
}
How would I change this line accordingly: Tutor = new Tutor { TutorNoID = enrolment.Tutor.TutorNoID, TutorCommissionPercentage = new TutorCommissionPercentage { CommissionPercentage = enrolment.Tutor.TutorCommissionPercentage } } now that I've made TutorCommissionPercentage
a property of Tutor?
And in this line: CommissionAmount = s.Sum(u => u.CommissionAmount) * s.Key.TutorCommissionPercentage, it says cannot implicitly convert type long to decimal. How would I explicitly convert it?
Okay, I've change commission percentage to a double because I'm getting irritated with that error and I want to test if the method is working. Once I did that, the error went away, but now there is error here:
}).ToList;
Cannot convert method group 'ToList' to non-delegate type 'System.Collections.Generic.List<DFPProductions_Default.Models.TutorCommission>'.
Did you intend to invoke the method?
My code looks like this now, just as a recap:
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");
}
ThatsIT
Participant
1026 Points
388 Posts
Re: Bulk Database Entries - Commission
Oct 09, 2011 09:47 AM|LINK
var newCommissions =db.Enrollments.select(s => new TutorCommission()
{
CommissionAmount = enrolment.MonthlyFee,
CommissionMonth = month, // string constant
CommissionStatus = "Unpaid",
Tutor = new Tutor { TutorNoID = enrolment.Tutor.TutorNoID, TutorCommissionPercentage = new TutorCommissionPercentage { CommissionPercentage = enrolment.Tutor.TutorCommissionPercentage.CommissionPercentage } }
};
replace the code below with the code above,
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 = new TutorCommissionPercentage { CommissionPercentage = enrolment.Tutor.TutorCommissionPercentage.CommissionPercentage } }
};
ThatsIT
Participant
1026 Points
388 Posts
Re: Bulk Database Entries - Commission
Oct 09, 2011 09:55 AM|LINK
The second error was because we were working with a list and not the entity direct
now when we ae making a commission obect, we are adding the tutor object, and then adding the TutorCommissionPercentage object to the Tutor.
i have a question, why is the TutorCommissionPercentage a child object, should CommissionPercentage simply be a property of Tutor?
Are there more then one tutor comissions?
I think you will still get an error.
to fix, you can either make CommissionPercentage a property of Tutor and not a collection of child objects
or you can change this line enrolment.Tutor.TutorCommissionPercentage.CommissionPercentage
to
enrolment.Tutor.TutorCommissionPercentage.firstordefault.CommissionPercentage
amecily1
Member
128 Points
83 Posts
Re: Bulk Database Entries - Commission
Oct 09, 2011 10:24 AM|LINK
Okay I've made TutorCommissionPercentage a property of Tutor. The reason I had it alone was because I wanted to keep it confidential but I can still do that by making it a property.
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 = new TutorCommissionPercentage { CommissionPercentage = 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"); }How would I change this line accordingly: Tutor = new Tutor { TutorNoID = enrolment.Tutor.TutorNoID, TutorCommissionPercentage = new TutorCommissionPercentage { CommissionPercentage = enrolment.Tutor.TutorCommissionPercentage } } now that I've made TutorCommissionPercentage a property of Tutor?
And in this line: CommissionAmount = s.Sum(u => u.CommissionAmount) * s.Key.TutorCommissionPercentage, it says cannot implicitly convert type long to decimal. How would I explicitly convert it?
ThatsIT
Participant
1026 Points
388 Posts
Re: Bulk Database Entries - Commission
Oct 09, 2011 11:37 AM|LINK
Tutor = new Tutor { TutorNoID = enrolment.Tutor.TutorNoID, TutorCommissionPercentage = enrolment.Tutor.CommissionPercentage }
You mignt need to check this, as i normaly program in VB,
CommissionAmount = (decimal)s.Sum(u => u.CommissionAmount) * s.Key.TutorCommissionPercentage
or
CommissionAmount = (decimal)(s.Sum(u => u.CommissionAmount) * s.Key.TutorCommissionPercentage)
or
CommissionAmount = Convert.ToDecimal(s.Sum(u => u.CommissionAmount) * s.Key.TutorCommissionPercentage)
But thats poses another question, why would the answer be a Long, it should be a decimal, double or float, you may be losing your decimal places
amecily1
Member
128 Points
83 Posts
Re: Bulk Database Entries - Commission
Oct 09, 2011 11:46 AM|LINK
Okay, I've changed the proerty CommissionAmount to a double, but it's now telling me that
ThatsIT
Participant
1026 Points
388 Posts
Re: Bulk Database Entries - Commission
Oct 09, 2011 12:34 PM|LINK
This is why i like VB
it must be something like
CommissionAmount = (decimal)s.Sum(u => u.CommissionAmount) * s.Key.TutorCommissionPercentage
or
CommissionAmount = s.Sum(u => u.CommissionAmount) * (decimal)s.Key.TutorCommissionPercentage
amecily1
Member
128 Points
83 Posts
Re: Bulk Database Entries - Commission
Oct 09, 2011 12:45 PM|LINK
Hmm, I can't get it work. Surely multiplying a double and a decimal should be allowed? That's something that needs to be done all the time!
ThatsIT
Participant
1026 Points
388 Posts
Re: Bulk Database Entries - Commission
Oct 09, 2011 12:50 PM|LINK
try
CommissionAmount = Decimal.Mutiply(s.Sum(u => u.CommissionAmount) , s.Key.TutorCommissionPercentage)
ThatsIT
Participant
1026 Points
388 Posts
Re: Bulk Database Entries - Commission
Oct 09, 2011 01:09 PM|LINK
I am jumping the gun herre, as i assume you are going to get the "can not convert to stored procedure" error
var newCommissions = db.Enrollments.asEnumerable.select(s => new TutorCommission()
{
CommissionAmount = enrolment.MonthlyFee,
CommissionMonth = month, // string constant
CommissionStatus = "Unpaid",
Tutor = new Tutor { TutorNoID = enrolment.Tutor.TutorNoID, enrolment.Tutor.CommissionPercentage }
};
Just a tip. thought in this case it does not matter.
Linq does not bother the database until a value is used,
this lne pulls the whole table into a list
var enrolments = db.Enrollments.ToList();
We may not need all the table, so it is better to do as i have done above "var newCommissions = db.Enrollments"
you can write a lot of linq code and have it compiled into one call to the database, very efficient.
Also AsEnumerable also calls the database, but it is sometimes necessary. thing to remember is to use it after any where clause if you can
the following line only pulls nine records from the database
var newCommissions = db.Enrollments.where(a => a.id < 10).AsEnumerable.select(s =>.....
This line pulls all records from the database
var newCommissions = db.Enrollments.AsEnumerable.where(a => a.id < 10).select(s =>.....
The reason we sometimes need asenumerable is because we need to use some code that cannot be converted into sql
amecily1
Member
128 Points
83 Posts
Re: Bulk Database Entries - Commission
Oct 09, 2011 10:15 PM|LINK
Okay, I've change commission percentage to a double because I'm getting irritated with that error and I want to test if the method is working. Once I did that, the error went away, but now there is error here:
}).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"); }