我是LINQ的新手,我对如何在LINQ中编写IF ELSE有点困惑。如何编写多条件的LINQ
,我已经创建当前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();
现在我想获得从列表中DOB和计算基于今天的日期目前的年龄,然后将其归类到年龄组(当前版本直接从数据库中获取年龄段)。
可用年龄组是:
低于25(ID为1)
26-35(ID是2时)
36-45(ID是3)
46-55(ID是4)
5 5及以上(ID被5)
例如,如果构件是DOB 1990年1月15日,他属于agegroup 2. 如果构件是DOB一九七〇年十二月二十日,他是属于agegroup 4.
我stucked在此编码:
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();
是你有人能帮助我吗?谢谢 !
UPDATE:
我有更新的想法使用LINQ当按钮点击所有行。例如,用户点击一个按钮,然后系统会检查每个人的DOB,然后在数据库中更新他们的年龄组。
例如,如果人物A DOB是1990-01-01,他/她的agegroup将自动更新为2,如果人物B DOB是1970年5月15日,他/她的agegroup将更新至4
如何编写一个linq来更新数据库中的所有行? 谢谢你的帮助!
好吧,我不会试图写有嵌套条件运营商所有的逻辑。只需编写一个计算年龄的方法来检查它所在的乐队并返回相应的ID。 – juharr
你最好只是在你的数据库中添加计算列:https://technet.microsoft.com/en-us/library/ms191250(v=sql.105).aspx – MarcinJuraszek