LINQ引入新领域
我有以下内容,
var mylist= (from inlst in db.Progs
where inlst.ProgID == PGID
select new
{
Date = inlst.Date1,
Name = inlst.Reps1,
Cancel = inlst.Cancel,
Reason = inlst.Reason,
Status = ...
}).ToList();
我想引入一个不在表中的新字段,称为 status
也就是说:
- if Date is not null then status = date else
- if Name is not null then status = name else
- if Cancel not null then status = Reason else
- status = cancel
我不知道如何使用 if 语句引入新字段
I have the following
var mylist= (from inlst in db.Progs
where inlst.ProgID == PGID
select new
{
Date = inlst.Date1,
Name = inlst.Reps1,
Cancel = inlst.Cancel,
Reason = inlst.Reason,
Status = ...
}).ToList();
I would like to introduce a new field not in the table called status
that says:
- if Date is not null then status = date else
- if Name is not null then status = name else
- if Cancel not null then status = reason else
- status = cancel
I am not sure how to introduce a new field though with if statement
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用简单的
Status = inlst.Date != null...
来完成此操作,但我会让它更像这样
Typed
。创建一个类
创建可能的状态值的枚举
然后像这样选择它
希望这有帮助
You can do this with a simple
Status = inlst.Date != null...
But i would make it a little more
Typed
like this.create a class
create a enum of your possible status values
then select it like this
hope this helps
使用条件运算符的简写形式
:
Use short form of conditional operator
: