LINQ:使用三元(?:)与LINQ let是不够的,需要一个“IF”但似乎不能让它上班 [英] LINQ: Using a ternary ( ?: ) with LINQ let is not enough, need an "IF" but can't seem to get it to work

查看:99
本文介绍了LINQ:使用三元(?:)与LINQ let是不够的,需要一个“IF”但似乎不能让它上班的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在LINQ的LET中包含一个IF,但是我不能让它工作,似乎对三元运算符起作用,但这是TRUE或FALSE,我需要有两个以上的选项。



我认为这很好解释



基本上我有一个选择,使用数据库连接来选择项目。然后我得到每个记录的状态,但是我必须根据products.type的类型,在单独的表中进行加入。

  var tst = from p in products join i in info on p.id equals i.pid 

// if p.type =homethen ...

let status = from s in homestatus
select new {status = s.status}

//如果p.type =offshore然后


let status = from s in offshorestatus
select new {status = s.status}


//如果p.type =internal则


let status = from s in internalestatus
select new {status = s.status}

选择新的{
name = p.name,
status = status.StatusText
}

任何人都有任何想法如何做一个标准的IF所以我可以选择我希望执行哪个STATUS(let)。



提前感谢

解决方案

您可以使用条件运算符(1)

  var tst = from p in products join i in info on p.id equals i.pid 

let status = p.type =home? homestatus.Select(s => s.status):
p.type =offshore? offshorestatus.Select(s => s.status):
p.type =internal? internalestatus.Select(s => s.status):null
选择新的{
name = p.name,
status = status!= null? status.StatusText:string.Empty;
}

如果您不使用StatusText以外的其他状态,您还可以这样做

  var tst = from p in products join i in info on p.id equals i.pid 

let status =(p.type =home?homestatus.Select(s => s.status.StatusText):
p.type =offshore?offshorestatus.Select(s =   s.status.StatusText):
p.type =internal?internalestatus.Select(s => s.status.StatusText):null)? string.Empty
选择新的{
name = p.name,
status = status;
}

(1)一个三元运算符是任何运算符这需要三个参数,其中目前在C#中只有一个称为条件运算符


I am trying to include an IF within my LET in LINQ but i can't get it to work, it seems to work for the ternary operator, but this is TRUE or FALSE and i need to have more than 2 options.

I think this explains it well

Basically i have a select which selects items using joins from a DB. Then the i get the status for each record but i have to make a join on separate tables depending on the type from products.type

var tst = from p in products join i in info on p.id equals i.pid

// if p.type = "home" then ...

let status = from s in homestatus
select new { status = s.status }

// if p.type ="offshore" then


let status = from s in offshorestatus
select new { status = s.status }


// if p.type ="internal" then


let status = from s in internalestatus
select new { status = s.status }

select new {
name = p.name,
status = status.StatusText
}

Anybody have any ideas how to do a standard IF so i can select which STATUS (let) i wish to execute.

Thanks in advance

解决方案

You can do that with the conditional operator (1)

var tst = from p in products join i in info on p.id equals i.pid

let status = p.type = "home" ? homestatus.Select(s=>s.status) :
             p.type = "offshore" ? offshorestatus.Select(s=>s.status) :
             p.type = "internal" ? internalestatus.Select(s=>s.status) : null
select new {
name = p.name,
status = status != null ? status.StatusText : string.Empty;
}

If you are not using the status for anything else than the StatusText you could also do it like this

var tst = from p in products join i in info on p.id equals i.pid

let status = (p.type = "home" ? homestatus.Select(s=>s.status.StatusText) :
             p.type = "offshore" ? offshorestatus.Select(s=>s.status.StatusText) :
             p.type = "internal" ? internalestatus.Select(s=>s.status.StatusText) : null) ?? string.Empty
select new {
name = p.name,
status = status;
}

(1) A ternary operator is any operator that takes three arguments, of which there at present is only one in C# called the conditional operator

这篇关于LINQ:使用三元(?:)与LINQ let是不够的,需要一个“IF”但似乎不能让它上班的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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