【问题标题】:How to Write LINQ with multiple condition如何编写具有多个条件的 LINQ
【发布时间】:2017-02-08 01:49:13
【问题描述】:

我是 LINQ 新手,对如何在 LINQ 中编写 IF ELSE 有点困惑。

我创建的当前 LINQ 是:

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();

现在我想从列表中获取出生日期并根据今天的日期计算当前年龄,然后将其分类到年龄组中(当前版本是直接从数据库中获取年龄组)。

可用的年龄组是:

  • 低于 25(ID 为 1)

  • 26-35(ID为2)

  • 36-45(ID为3)

  • 46-55(ID为4)

  • 55 及以上(ID 为 5)

例如,如果成员 DOB 是 1990-01-15,则他属于年龄组 2。 如果成员 DOB 是 1970-12-20,他属于年龄组 4。

我被这个编码困住了:

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();

你们中的任何人都可以帮助我吗?谢谢!

更新:

我想在单击按钮时使用 LINQ 更新所有行。例如,如果用户点击了一个按钮,那么系统会检查每个人的出生日期,然后在数据库中更新他们的年龄组。

例如,如果 A 人 DOB 是 1990-01-01,他/她的年龄组将自动更新为 2,如果 B 人 DOB 是 1970-05-15,他/她的年龄组将自动更新为 4。

如何编写一个 linq 来更新数据库中的所有行? 感谢您的帮助!

【问题讨论】:

标签: c# linq


【解决方案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();

【讨论】:

  • 感谢您的建议,但我有另一个想法是更新表“人”中的数据库 agegroup_id 列。例如,当系统启动时,数据库运行一个 linq,它会根据今天的日期自动更新所有行。我已经编写了根据今天的日期计算当前年龄的计算方法。但是你能教我如何更新表“人”中的多行吗?我将在上面的问题中写更多细节。非常感谢你! @里克
  • 你在这段代码中使用了哪个 ORM?EF 还是 Linq2sql?foreach(var p in lstRecord){p.AgeGroupID=1?2?3...} db.SubmitChanges();(Linq2Sql ),ctx.SaveChanges();(EF)
  • 如果表'person'的数据很大,那么这不是一个好主意。这只是我的建议。
【解决方案2】:
lstRecord = (from a in lstPerson
             select new GeneralCommonFunctions.MemberDetailSumary
             {
               Age = DateTimeUtility.GetCurrentAge(a.Dob),
               AgeGroupID = GetAgeGroup(a.Dob);

 }).ToList();

您可以创建方法GetAgeGroup,在其中您将提供当前的出生日期。根据您的解释a.DobDateTime。之后,您将计算此人的年龄并输入正确的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
}

【讨论】:

  • 感谢您的建议,但我有另一个想法是更新表“人”中的数据库 agegroup_id 列。例如,当系统启动时,数据库运行一个 linq,它会根据今天的日期自动更新所有行。我已经编写了根据今天的日期计算当前年龄的计算方法。但是你能教我如何更新表“人”中的多行吗?我将在上面的问题中写更多细节。非常感谢你! @mybirthname
  • 这段代码很奇怪......为什么你已经检查年龄是否超过25,而你已经检查它小于或等于25? if(age&lt;=25) return 1; else if(age&lt;=35) return 2; else if(age&lt;=45) return 3; else if(age&lt;= 55) return 4; else return 5
猜你喜欢
  • 1970-01-01
  • 2013-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多