C
C#2y 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
Kouhai2y ago
What problem are you facing? @Guns
Pobiega
Pobiega2y 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
GunsOP2y 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
Pobiega2y ago
What is wrong with it? Seems fine. If you fix your constructor, it should worl
Guns
GunsOP2y ago
what should i fix in it?
Pobiega
Pobiega2y ago
Read it again, and check what is on what side of the equals sign
Guns
GunsOP2y ago
so is this ok; 'new DateTime(2000, 5, 1))'. thats how i define the date property for an obj?
Pobiega
Pobiega2y ago
Sure
Guns
GunsOP2y ago
ohh i thought it was wrong
Pobiega
Pobiega2y ago
I'm not at a computer right now, but there sure is a date time constructor that looks something like that
Guns
GunsOP2y ago
'where emp.HireDate.Year > 2005' so this linq query should work?
Pobiega
Pobiega2y ago
Yup, but it will look only on year So 2005 december will not count
Guns
GunsOP2y ago
ok ill have a look into that gimme a sec
Kouhai
Kouhai2y ago
Couple of things to consider as well You should refactor Name property to be an auto property, also remove hiredatepublic field
Pobiega
Pobiega2y ago
And the names in the ctor makes me very upset
Guns
GunsOP2y ago
i fix that
Kouhai
Kouhai2y ago
underscores 😅
Guns
GunsOP2y ago
i always make the dumbest mistakes lol thanks guys gg
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