C
C#2y ago
Ice_

❔ Entity Framework. Rental list

I have a class for books and I have a class of customers. I also have a class for loans. The books should be possible to be loaned by other customers as well but only after the loaner has given it back and the loaner can choose themselves how many days they want to loan the book, a maximum of a week though. The loans class should be a junction table, I assume this is a many-to-many relation? I can figure out the relation-stuff in EF but I'm unsure how to check the timespan between date1 and date2. It's of course of type DateTime. It seems like I can use calculus to calculate the datetimes but how would this look like with either Linq or with a lambda expression?
8 Replies
Anton
Anton2y ago
add a getter for that time span in your class, and just => date2 - date1; calculate the difference there you're right, you need a junction table with (loan id, person id, book id) (if you keep the loan history, if you don't, then you don't need the loan id) the simplest way to set this up would be to make a class Loan with those three and set up the relations using the fluent builder then add loan navigation properties as needed to people or books setting up the relations should be pretty trivial look up the docs and go through the relevant page
Anton
Anton2y ago
just read through this I recommend you understand the ideas thoroughly
Ice_
Ice_OP2y ago
Thank you! Not sure what you mean with the getter though. I know you mean using the property in get; set; but not sure what => date2 - date1 means.
ringsig
ringsig2y ago
If you're storing LoanedOn and RepaidOn, you can calculate LoanDuration using RepaidOn and LoanedOn. Traditionally you would use a method to calculate LoanDuration like GetLoanDuration() but C# allows you to use 'getters' which are actually methods but you use them like properties (technically they are properties)
public class Loan
{
public DateTime LoanedOn { get; set; }
public DateTime RepaidOn { get; set; }

public TimeSpan LoanDuration => RepaidOn - LoanedOn;
}
public class Loan
{
public DateTime LoanedOn { get; set; }
public DateTime RepaidOn { get; set; }

public TimeSpan LoanDuration => RepaidOn - LoanedOn;
}
if you have a variable loan, you can access loan.LoanDuration to get the duration. This way you don't have to store LoanDuration separately in a column which would lead to data redundancy (not a good thing—it can lose sync and cause weird bugs)
Ice_
Ice_OP2y ago
Thank you ringsig! Doesn't that Loan object have to have an id? Or am I misinterpret something? 🙄 But I do get your timespan property! Is there also a difference between saying: LoanDuration => RepaidOn - LoanedOn; LoanDuration = RepaidOn - LoadedOn:
Anton
Anton2y ago
the second one is an initializer that's run before the constructor they just showed the relevant bit of the class
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server