我知道这个问题已在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#,但是我无法将其正确转换为vb.net我试过telerik 但没有运气。
我无法在我的if语句中调用/使用linq select new {}中的'p'。
答案 0 :(得分:0)
只需选择所需的值即可。匿名类型隐含在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
答案 1 :(得分:0)
您似乎遇到两个问题:匿名类型和lambdas - 下面的转换应该澄清这些问题: 选项推断
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