C
C#17mo ago
Guns

❔ Need help with datetime

I need to create a class which has two fields; name and date. Then create objects of this class. I need to use datetime for the date field. anyone knows what im doing wrong? so basically; static void Main() { //create intances of Employee; Employee emp1 = new Employee("James", 25, new DateTime(2000, 5, 1)); //add employees to list List<Employee> employees = new List<Employee>(4); employees.Add(emp1); //LINQ Questions; //Q1. Return a List of employees that hired after the year 2005 var queryHiredAfter2005 = from emp in employees where emp.HireDate.Year > 2005 select emp.Name; foreach (var res in queryHiredAfter2005) { Console.WriteLine(res); } class Employee { public string name; public string Name { get { return name; } set { name = value; } } public DateTime hiredate; public DateTime HireDate { get; set; } //Constructor; public Employee(string _name, int _age, DateTime _hiredate) { _name = Name; _age = Age; _hiredate = HireDate; }
19 Replies
Kouhai
Kouhai17mo ago
What problem are you facing? @Guns
Pobiega
Pobiega17mo ago
You don't seem to have a prop for age, and your constructor is... Wrong Your naming convention is inverted Name should be an auto prop
Guns
Guns17mo ago
with the datetime prop yes ill fix later the main issue is the datetime i need to have datetime as a prop how does that work
Pobiega
Pobiega17mo ago
What is wrong with it? Seems fine. If you fix your constructor, it should worl
Guns
Guns17mo ago
what should i fix in it?
Pobiega
Pobiega17mo ago
Read it again, and check what is on what side of the equals sign
Guns
Guns17mo ago
so is this ok; 'new DateTime(2000, 5, 1))'. thats how i define the date property for an obj?
Pobiega
Pobiega17mo ago
Sure
Guns
Guns17mo ago
ohh i thought it was wrong
Pobiega
Pobiega17mo ago
I'm not at a computer right now, but there sure is a date time constructor that looks something like that
Guns
Guns17mo ago
'where emp.HireDate.Year > 2005' so this linq query should work?
Pobiega
Pobiega17mo ago
Yup, but it will look only on year So 2005 december will not count
Guns
Guns17mo ago
ok ill have a look into that gimme a sec
Kouhai
Kouhai17mo ago
Couple of things to consider as well You should refactor Name property to be an auto property, also remove hiredatepublic field
Pobiega
Pobiega17mo ago
And the names in the ctor makes me very upset
Guns
Guns17mo ago
i fix that
Kouhai
Kouhai17mo ago
underscores 😅
Guns
Guns17mo ago
i always make the dumbest mistakes lol thanks guys gg
Accord
Accord17mo 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.