LINQ WHERE使用if语句条款语句、条款、LINQ、WHERE

2023-09-03 16:35:47 作者:苁吥~强留

我使用C#.NET

我有两个文本框这要是!空需要是一个LINQ查询中的WHERE子句的一部分。

I have two textboxes which if !empty need to be part of a WHERE clause within a LINQ query.

下面是我的code

var result = from a in xxxx select a;

if(!string.IsNullOrEmpty(personName))
{
    return result.Where(a >= a.forename.Contains(personName) || a.surname.Contains(personName)     
}
else if(!string.IsNullOrEmpty(dateFrom))
{
    return result.Where(a >= a.appStartDateTime >= dateFrom.Date)
}
else if(!string.IsNullOrEmpty(personName) && !string.IsNullOrEmpty(dateFrom))
{
    return result.Where(a >= a.forename.Contains(personName) || a.surname.Contains(personName) &&   a.appStartDateTime >= dateFrom.Date);
}

我想这会工作,但它不喜欢的。凡我不能访问一个为例a.forename(名称'一'在目前的情况下不存在)

I thought this would work but it doesn't like the .Where and I cant access the 'a' for example a.forename (The name 'a' does not exist in the current context)

我怎么错了,或者这实际上不能做?

What am I going wrong, or can this not actually be done?

在此先感谢您的帮助。

克莱尔

推荐答案

而不是这样的:

result.Where(a.forename.Contains(personName))

试试这个:

result.Where(a => a.forename.Contains(personName))

您似乎缺少拉姆达运算符(=>)。

You appear to be missing the Lambda operator (=>).