简体   繁体   English

vb.net linq多重搜索过滤器的where子句-相同的查询但不同的where子句

[英]vb.net linq where clause for mutiple search filter - same query but different where clause

I know this question was answered in c# but when I try to covert it online from c# to vb.net I got running errors. 我知道这个问题在C#中得到了回答,但是当我尝试从C#到vb.net在线对其进行隐蔽处理时,出现运行错误。

disclaimer this is not my code 免责声明这不是我的代码

   string personName = txtPersonName.Text;
   int personAge = Convert.ToInt32(txtAge.Text);
   var opportunites =  from p in this.DataContext.Persons
                        select new
                        {
                            p.PersonID,
                            p.Name,
                            p.Age,
                            p.Gender
                        };

    if (personsID != 0)
        opportunites = opportunites.Where(p => p.PersonID == personID);

    if (personName != string.Empty)
        opportunites = opportunites.Where(p => p.Name.StartsWith(personName));

    if (personAge != 0)
        opportunites = opportunites.Where(p => p.Age == personAge);

can someone help me to convert this code I think it is c#, but can I can't convert it properly to vb.net i tried telerik but no luck. 有人可以帮我转换此代码,我认为它是c#,但是我不能正确地将其转换为vb.net

I can't call/use the 'p' in linq select new {} on my if statement. 我无法在if语句中调用/使用linq中的'p'选择new {}。

just select the values you want. 只需选择所需的值即可。 the anonymous type is implied in vb vb中隐含了匿名类型

https://msdn.microsoft.com/en-us/library/bb384767.aspx https://msdn.microsoft.com/zh-CN/library/bb384767.aspx

Dim personName As String = "Joe"
Dim personAge As Integer = Convert.ToInt32(5)
Dim opportunites = From p IN AspNetUsers
                   Select p.Id,
                          p.FirstName,
                          p.LastName

If (personAge <> 0)
    opportunites = From o In opportunites Where o.Id >= 10
End If

There are two areas you seem to be having problems with: the anonymous type and the lambdas - the conversion below should clarify these issues: Option Infer On 您似乎在两个方面遇到问题:匿名类型和lambdas-下面的转换应阐明这些问题:Option Infer On

Dim personName As String = txtPersonName.Text
Dim personAge As Integer = Convert.ToInt32(txtAge.Text)
Dim opportunites = From p In Me.DataContext.Persons
    Select New With {
        Key p.PersonID,
        Key p.Name,
        Key p.Age,
        Key p.Gender
    }

If personsID <> 0 Then
    opportunites = opportunites.Where(Function(p) p.PersonID = personID)
End If

If personName <> String.Empty Then
    opportunites = opportunites.Where(Function(p) p.Name.StartsWith(personName))
End If

If personAge <> 0 Then
    opportunites = opportunites.Where(Function(p) p.Age = personAge)
End If

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM