开发者

filtering age of members using linq

I am trying to check for people in a certain range - let minage and maxage corresponding for e.g. age group with minage=18 and maxage =24 meaning I am trying to filter out the people aged between 18 and 24.Also datatype for dob of member is string ..

i am trying this

int agefrom = Convert.ToInt32(cbGEFrom.Text);
int ageto = Convert.ToInt32(cbGETo.Text);
DateTime today = DateTime.Today;
DateTime max 开发者_JS百科= today.AddYears(-(agefrom + 1));
DateTime min = today.AddYears(-(ageto));
string maxage = Convert.ToString(max);
string minage = Convert.ToString(min);

var members = 
    from report in eclipse.members 
    where string.Compare(report.member_Dob,minage) >=0
    where string.Compare(report.member_Dob,maxage) < 0
    select report;

i dont know is this way is correct or not ..

would any pls suggest any idea ..

many thanks in advance...


    int agefrom = Convert.ToInt32(cbGEFrom.Text);
    int ageto = Convert.ToInt32(cbGETo.Text);

    DateTime today = DateTime.Today;
    DateTime maxDOB = today.AddYears(-ageTo);
    DateTime minDOB = today.AddYears(-ageFrom);

    var members = eclipse.members.Where(m=>m.member_Dob>=minDOB && m.member_Dob<=maxDOB);

EDIT: Query comprehension is as under

    var members = from member in eclipse.members
                  where member.member_Dob>=minDOB && member.member_Dob<=maxDOB
                  select member;


Comparing dates via their string value feels horridly wrong to me.

You say the type of the member_Dob field is string; I'd say there is something wrong with your model, it should be DateTime

If you can't change the underlying model, at least convert it in code (using DateTime.Parse). Then you can simply compare DateTimes in your where clause as per the other answers:

var members = from report in eclipse.members
          let dob =  DateTime.Parse(report.member_Dob) // or use ParseExact 
          where  dob >= minDOB && dob < maxDOB 
          select report;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜