C
C#11mo ago
ninja239

❔ How do I use LINQ to get Friends younger than 50?

I have a class Friend:
class Friend
{
public DateTime BirthDate;
public string PhoneNumber;
public string Name;
public string Id;
public Friend(string name, string BirthDate, string PhoneNumber)
{
this.Name = name;
this.BirthDate = DateTime.Parse(BirthDate);
this.PhoneNumber = PhoneNumber;
this.Id = Guid.NewGuid().ToString();
}
public Friend(string name, DateTime BirthDate, string PhoneNumber)
{
this.Name = name;
this.BirthDate = BirthDate;
this.PhoneNumber = PhoneNumber;
this.Id = Guid.NewGuid().ToString();
}
}
class Friend
{
public DateTime BirthDate;
public string PhoneNumber;
public string Name;
public string Id;
public Friend(string name, string BirthDate, string PhoneNumber)
{
this.Name = name;
this.BirthDate = DateTime.Parse(BirthDate);
this.PhoneNumber = PhoneNumber;
this.Id = Guid.NewGuid().ToString();
}
public Friend(string name, DateTime BirthDate, string PhoneNumber)
{
this.Name = name;
this.BirthDate = BirthDate;
this.PhoneNumber = PhoneNumber;
this.Id = Guid.NewGuid().ToString();
}
}
I have a list of friends called "friends":
var friends = new List<Friend>{
new Friend("Linus", "01/01/1970", "707-419-4447"),
new Friend("Scott", "10/10/1970", "801-224-7513"),
new Friend("Charlie", DateTime.Parse("01/01/1999"), "801-225-8597")
};
var friends = new List<Friend>{
new Friend("Linus", "01/01/1970", "707-419-4447"),
new Friend("Scott", "10/10/1970", "801-224-7513"),
new Friend("Charlie", DateTime.Parse("01/01/1999"), "801-225-8597")
};
and I want to get all Friends whose birth dates were less than 50 years ago. To do that, I say:
var youngFriendsQuery =
from friend in friends
where (friend.BirthDate - now).TotalDays < 50*365
select friend;
var youngFriendsQuery =
from friend in friends
where (friend.BirthDate - now).TotalDays < 50*365
select friend;
but apparently all 3 Friends match. How do I fix this?
5 Replies
SinFluxx
SinFluxx11mo ago
Note that 50 * 365 != 50 years, leap years are a thing You'd also want to do Now - Birthdate, otherwise you'll get a negative number Though really you'd want to just do Datetime.Now.AddYears(-50) and check their Birthdate is after that, then you don't have to worry about the above 2 points
ninja239
ninja23911mo ago
thx So something like this:
// get all friends younger than 50.
var now = DateTime.Now;
var youngFriendsQuery =
from friend in friends
where (friend.BirthDate > now.AddYears(-50))
select friend;
// get all friends younger than 50.
var now = DateTime.Now;
var youngFriendsQuery =
from friend in friends
where (friend.BirthDate > now.AddYears(-50))
select friend;
SinFluxx
SinFluxx11mo ago
yeah I would think that should do the trick 👍 have a play to make sure
TheBoxyBear
TheBoxyBear11mo ago
One thing to consider is if the input dates include a time, the age calculation will take that into accout and can give unexpected results if it's their birthday baeed on the day but not quite time wise You could consider using DateOnly to avoid such problems There's also TimeOnly for when the date doesn't matter.
Accord
Accord11mo 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.