8,497
社区成员
发帖
与我相关
我的任务
分享
var query=(from s in dc.students
select s).ToList();
query=query.Where(s=>GetAge(s.birth)>18 && GetAge(s.birth)<25);
int GetAge(string birthday)
{
try
{
return DateTime.Now.Year-Convert.ToDateTime(birthday).Year;
}
catch {return 0;}
}
class User
{
public int user_id;
public string user_name;
public string user_birth;
}
private void Form1_Load(object sender, EventArgs e)
{
List<User> users = new List<User>{
new User{user_id = 0, user_name = "000", user_birth = new DateTime(1978,2,3).ToString()},
new User{user_id = 1, user_name = "111", user_birth = new DateTime(1988,2,3).ToString()},
new User{user_id = 2, user_name = "222", user_birth = new DateTime(2008,2,3).ToString()}
};
var query = from s in users
where GetAge(s.user_birth) > 18 && GetAge(s.user_birth) < 25
select s;
foreach (User u in query)
{
Console.WriteLine(u.user_name + " " + u.user_birth);
}
}
int GetAge(string birthday)
{
try
{
int age = DateTime.Now.Year - Convert.ToDateTime(birthday).Year;
return age;
}
catch { return 0; }
}