1

I am new to LINQ and I am a bit confuse on how to write IF ELSE in LINQ.

The current LINQ that I have created is :

lstRecord = (from a in lstPerson
            select new GeneralCommonFunctions.MemberDetailSumary
            {
              AgeGroupId = a.AgeGroup_id!=null? a.AgeGroup_id.Value: 0,
              AgeGroupText = a.Agegroup!=null?a.Agegroup.Description: "",
            }).ToList();

Now I would like to get the DOB from the list and calculate the current age based on today's date, then categorise it into the age group (the current version is directly get the agegroup from database).

The available age group is :

  • BELOW 25 (ID is 1)

  • 26-35 (ID is 2)

  • 36-45 (ID is 3)

  • 46-55 (ID is 4)

  • 55 AND ABOVE (ID is 5)

For example, if member DOB is 1990-01-15, he is belongs to agegroup 2. if member DOB is 1970-12-20, he is belongs to agegroup 4.

I am stucked in this coding:

lstRecord = (from a in lstPerson
             select new GeneralCommonFunctions.MemberDetailSumary
             {
               Age = DateTimeUtility.GetCurrentAge(a.Dob),
               //I dont know how to continue in this part, my idea is if the age is 0-25, the agegroup_id is 1, if the age is 30, the agegroup_id is 2.

 }).ToList();

Is anyone of you can help me with this? Thank you !

UPDATE:

I have an idea of update all the rows using LINQ when button clicked. For example, is user clicked a button, then the system will check every person DOB, then update their agegroup in database.

For example, if person A DOB is 1990-01-01, his/her agegroup will automatically update to 2, if person B DOB is 1970-05-15, his/her agegroup will update to 4.

How to write a linq to update all rows in database? Thank you for your help!

2
  • Well I wouldn't try to write all that logic with nested conditional operators. Just write a method that calculates that age checks the band it's in and returns the appropriate id. Commented Feb 8, 2017 at 2:04
  • You'd be better just adding computed column in your database: technet.microsoft.com/en-us/library/ms191250(v=sql.105).aspx Commented Feb 8, 2017 at 2:38

2 Answers 2

1
lstRecord = (from a in lstPerson
         select new GeneralCommonFunctions.MemberDetailSumary
         {
           Age = DateTimeUtility.GetCurrentAge(a.Dob),
         }).Select(t=>new GeneralCommonFunctions.MemberDetailSumary{
                      Age=t.Age,
                      AgeGroupID = t.Age<=25?1:t.Age<=35?2:t.Age<=45?3:t.Age<=55?4:5,
                  }).ToList();
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your suggestion but I have another idea which is to update database agegroup_id column in table 'person'. For example, when system start, the database run a linq which is automatically update all the rows based on today date. I already wrote the calculation to calculate the current age based on today's date. But can you teach me on how to update multiple rows in table 'person'? I will wrote more details in the question above. Thank you very much! @Rick
Which ORM are you use in this code?EF or Linq2sql?foreach(var p in lstRecord){p.AgeGroupID=1?2?3...} db.SubmitChanges();(Linq2Sql),ctx.SaveChanges();(EF)
if the table 'person's data is large,then this is not a good idea. it is just my Suggest.
1
lstRecord = (from a in lstPerson
             select new GeneralCommonFunctions.MemberDetailSumary
             {
               Age = DateTimeUtility.GetCurrentAge(a.Dob),
               AgeGroupID = GetAgeGroup(a.Dob);

 }).ToList();

You could create method GetAgeGroup in which you will give the current birth date. From your explanation a.Dob is DateTime. After that you are calculating the age of the person and put the correct AgeGroupID

public int GetAgeGroup(DateTime birthYear)
{
    //here you can reuse your method DateTimeUtility.GetCurrentAge(a.Dob),
    // I just wrote the logic because I'm not sure if it is correct.
    int age= DateTime.Now.Years - birthYear.Years;

    if (birthYear > DateTime.Now.AddYears(-years))
        age--;

    if(age<=25)
        return 1;
    else if( age> 25 && age<=35)
        return 2;
    else if(age> 35 && age<=45)
        return 3;
    else if(age> 45 && age<= 55)
        return 4;
    else if(age> 55)
        return 5
    else
        return -1; // this will represent invalid age
}

2 Comments

Thank you for your suggestion but I have another idea which is to update database agegroup_id column in table 'person'. For example, when system start, the database run a linq which is automatically update all the rows based on today date. I already wrote the calculation to calculate the current age based on today's date. But can you teach me on how to update multiple rows in table 'person'? I will wrote more details in the question above. Thank you very much! @mybirthname
This code is strange... why do you check if age is more than 25 when you already checked it was under or equal to 25? if(age<=25) return 1; else if(age<=35) return 2; else if(age<=45) return 3; else if(age<= 55) return 4; else return 5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.