vb.net linq where 子句用于多个搜索筛选器 - 相同的查询但不同的 where 子句
本文关键字:where 子句 查询 搜索 linq net 用于 vb 筛选 | 更新日期: 2023-09-27 18:32:26
我知道这个问题在 c# 中得到了回答,但是当我尝试从 c# 在线掩盖它时 vb.net 我遇到了运行错误。
免责声明这不是我的代码
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);
有人可以帮我转换这段代码,我认为它是 C#,但我无法将其正确转换为我尝试 telerik vb.net但没有运气。
我不能在我的 if 语句上调用/使用 linq 中的"p"选择新的 {}。
您似乎在两个方面遇到了问题:匿名类型和 lambda - 下面的转换应该澄清这些问题:选项推断开启
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
只需选择所需的值即可。 匿名类型隐含在 VB 中
https://msdn.microsoft.com/en-us/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