Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am having a Semester1 including StartDate1 and EndDate1. I want to create a new Semester2 and it must ensure EndDate1 < StartDate2. How can I compare them?

I am using .Net Core 5 and Repository pattern

My code:
_dbContext.Semesters.Find(semesterViewModel.Semester.SemesterEndDate) < semesterViewModel.Semester.SemesterStartDate


What I have tried:

I have tried Get EndDate for assignment however it also failed with DateTime

EDIT: Code added from "solution" 2:
C#
public SemesterViewModel CreateSemester(SemesterViewModel semesterViewModel)
{
    if (CheckExistSemester(semesterViewModel.Semester.SemesterName))
    {
        var model = new SemesterViewModel()
        {
            Semester = semesterViewModel.Semester,
            StatusMessage = "Error: Semester already exists!"
        };
        return model;
    }
    
    if (semesterViewModel.Semester.SemesterStartDate < semesterViewModel.Semester.SemesterClosureDate &&
        semesterViewModel.Semester.SemesterClosureDate < semesterViewModel.Semester.SemesterEndDate)
    {
        var newSemester = new Semester
        {
            SemesterName = semesterViewModel.Semester.SemesterName,
            SemesterStartDate = semesterViewModel.Semester.SemesterStartDate,
            SemesterClosureDate = semesterViewModel.Semester.SemesterClosureDate,
            SemesterEndDate = semesterViewModel.Semester.SemesterEndDate
        };
        _dbContext.Semesters.Add(newSemester);
        _dbContext.SaveChanges();
        return null;
    }
Posted
Updated 15-Mar-21 3:18am
v2
Comments
Richard Deeming 15-Mar-21 9:12am    
If you want to update your question to include missing details, click the green "Improve question" link and update your question.

Do not post your edit as a "solution" to your question.

What you are comparing isn't a pair of DateTime objects - it's (probably, we can;t see your code from here) a DateTime on the right hand side of the comparison, but the left hand side will be a collection of Semester objects.

Try it: split your line into two parts, so you assign the Find result to a variable:
C#
var x = _dbContext.Semesters.Find(semesterViewModel.Semester.SemesterEndDate);
And use x in your comparison.
Now look at exactly what type the system will give x - you had hover the mouse over it in VS for that - and work out from there where the value you want to compare is.

Sorry, but we can't do that for you - we can't see your screen, access your HDD, or read your mind, and we have no idea what you are actually trying to compare!
 
Share this answer
 
Comments
huynbt209 15-Mar-21 3:22am    
I will show my code. Wait for me a minute.
huynbt209 15-Mar-21 3:24am    
I show my code below, I execute it in my Repo
Quote:
C#
_dbContext.Semesters.Find(semesterViewModel.Semester.SemesterEndDate) < semesterViewModel.Semester.SemesterStartDate
The Find method finds the entity with the specified primary key, and returns it. That return value will be a Semester class instance, which could be null if no matching record was found.

You are then trying to test whether that Semester instance is less than a DateTime instance. But since you have not defined any way to compare a Semester instance to a DateTime, the code cannot compile.

It's not entirely clear what you are trying to do here, since the Semester is unlikely to have a DateTime for its primary key. At a complete guess, are you trying to ensure there are no overlapping semesters already in the database?
C#
bool anyOverlappingSemesters = _dbContext.Semesters.Any(s =>
    s.SemesterEndDate >= semesterViewModel.Semester.SemesterStartDate 
    && s.SemesterStartDate <= semesterViewModel.Semester.SemesterEndDate);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900