根据列的值,我尝试创建具有特定属性值的对象。如何修改以下查询来执行此操作? // object alertCode具有“code”和“description”属性的代码,具体取决于查询返回的alert_type值。
var medicalcodes = new string[]{"M", "D", "5"};
var specialedcodes = new string []{"C", "I"};
var personalcommentscodes = new string []{"A"};
var academiccodes = new string[]{"R", "E", "S"};
List<alertCode alertCollections = (from a in alertinfo
select new alertCode{
where medicalcodes.Contains(a.alert_type)
code = 'M',
where specialcodes.Contains(a.alert_type)
code = 'S',
where personalcommentscodes.Contains(a.alert_type)
code = 'P',
where academiccodes.Contains(a.alert_type)
code = 'A',
desc = a.description
} ).ToList();
答案 0 :(得分:1)
我已将代码计算移到单独的方法
var query = alertinfo.Select(a => new alertCode()
{
code = GetCodeFor(a.alert_type),
desc = a.description
}).ToList();
代码计算:
private char GetCodeFor(string alertType)
{
if (medicalcodes.Contains(alertType)
return 'M';
if (specialcodes.Contains(alertType)
return 'S';
if (personalcommentscodes.Contains(alertType)
return 'P';
if (academiccodes.Contains(alertType))
return 'A';
// return default value
}