有条件的LINQ where语句? [英] Conditional LINQ where statement?

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

问题描述

我有,我想增加一个额外的一个LINQ声明where子句,如果一个下拉指数不为0。

I have a linq statement that I want to add an additional where clause to if a drop down index is not 0.

people.Where(n.surname == "surname" || n.forename == "forename" && (dropdown.SelectedIndex > 0) ? n.id = dropdown.SelectedValue : n.id  > 0).Select(n => n);

我甚至不知道我想做的可能吗?

I am not even sure if what I am trying is possible??

我想做到这一点,而不是写两个不同的语句。

I would like to do this rather than having to write two different statements.

任何想法?

感谢

推荐答案

幸运的是,这很容易,因为查询组成:

Fortunately, this is easy because queries compose:

var query = people.Where(n.surname == "surname" || n.forename == "forename");
if (dropdown.SelectedIndex > 0)
{
    query = query.Where(n => n.id.ToString() == dropdown.SelectedValue);
}

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

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