-1

I has a table call Staff and has two field call StaffStatus and ParentStatus. I would like to display the Status as "Available" when StaffStatus AND ParentStatus are both "Available" and display the Status as "Unavailable" when either StaffStaus OR ParentStatus is "Unavailable".

Friend Function getAllUser(strInput As String) As IQueryable
getAllUser = From userDB In db.Staffs                    
             Select Status = userDB.UserStatus.Equals("Available") And userDB.ParentStatus.Equals("Available") ? "Available"  : (userDB.UserStatus.Equals("Unavailable") Or userDB.ParentStatus.Equals("Unavailable") ? "Unavailable")
End Function

But the code editor show : The character "?" cannot be used here and the Syntax error of first "(" at ": (userDB.UserStatus.Equals("Unavailable")". Any error with my code?

Jay Chuah
  • 114
  • 2
  • 13

1 Answers1

1

Simply: There is no ternary operator (as you use it) in VB.NET Take a look at here: Is there a conditional ternary operator in VB.NET?

You have to use a Dim foo as String = If(bar = uz, cat, dog) construct

Edit: Try this

Dim getAllUser = From userDb In db.Staffs
                 Select New Staff() With
                     {
                     .UserStatus = If(userDb.UserStatus.Equals("Available") And userDb.ParentStatus.Equals("Available"), "Available", If(userDb.UserStatus.Equals("Unavailable") Or userDb.ParentStatus.Equals("Unavailable"), "Unavailable", "SomeOtherValue"))
                     }
Community
  • 1
  • 1
Radinator
  • 1,422
  • 11
  • 45
  • but i wanna return it as IQueryable. This will return the type Staff? – Jay Chuah Oct 10 '16 at 07:33
  • you have 2 options: first would be to pack this in your function (I just wrote it this way since this is more natural for me than your old VBA/VBS style where you assign the value of the expression to your function) or second: simply add a **.AsQueryable()** at the end of the linq2sql command (don't forget to wrapt the expression in brackets) – Radinator Oct 10 '16 at 07:57
  • How about if i wanna make it a name, like .UserStatus As Status? What command should i write? Just like the .UserStatus show as title Status. – Jay Chuah Oct 10 '16 at 10:46
  • don't get what you mean? do you want to create a extension methode or just add a further condition – Radinator Oct 10 '16 at 10:49
  • sorry to confuse u. I want to display the result using the linq select statement in the dataGridView. I input the result using the code. `dgvUser.DataSource = helperUserCKJ.getAllUser(objUser.UserID)` Then for the query i would like to select all related column with a well display title. For example the column .UserStatus i want to display it with the a column name "Status". – Jay Chuah Oct 10 '16 at 11:27
  • The code i originally use is ` getAllUser = From userDB In db.Staffs Order By userDB.UserStatus, userDB.Organization.OrganizationName, userDB.Company.CompanyName, userDB.Department.DepartmentName, userDB.Role.RoleName Descending, userDB.UserID Where userDB.UserID IsNot strInput` – Jay Chuah Oct 10 '16 at 11:28
  • ` Select ID = userDB.UserID, Name = userDB.UserName, Gender = userDB.UserGender, Address = userDB.UserAddress1 & ", " & userDB.UserAddress2, State = userDB.UserState, Role = userDB.Role.RoleName, Organization = userDB.Organization.OrganizationName, Company = userDB.Company.CompanyName, Department = userDB.Department.DepartmentName, Status = userDB.UserStatus` – Jay Chuah Oct 10 '16 at 11:29
  • But now i wanna add the condition i mentioned before. I dun know how to write the linq statement... – Jay Chuah Oct 10 '16 at 11:30
  • to customize your datagrid colums, simply use the DisplayName Attribute (http://stackoverflow.com/a/6228673/6635287) – Radinator Oct 10 '16 at 12:10