LINQ SELECT中的IF语句 [英] IF statement inside a LINQ SELECT

查看:100
本文介绍了LINQ SELECT中的IF语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在选择新"中生成IF条件语句,该条件检查两个字段的值以填充属性.

I'm trying to produce an IF conditional statement inside of a "select new" that checks the values of two fields in order to fill a property.

from e in Employees
where e.EmployeeID == id
select new {
    EmployeeID = e.EmployeeID,
    EmployeeName = e.FirstName + " " + e.LastName,
    Status = (if e.col1.HasValue then "This Value" else if e.col2.HasValue then "Other Value")
}

列为空,因此列类型为DateTime?数据类型.

The columns are nullable and therefore the column types are DateTime? data types.

只有一列或另一列将具有日期时间值,而不是两者都具有.

Only one or the other column will have a datetime value, not both.

我该怎么做?

推荐答案

Status = e.col1.HasValue ? "This Value" : (e.col2.HasValue ? "Other Value" : null)

这篇关于LINQ SELECT中的IF语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆