none
Using Linq Coalesce or need a c# equivalent to ? operand RRS feed

  • Question

  • Everything in my code is working and returning what I would like it to, but I would like the UI to display "Yes" or "No" instead of "True" or "False".  Instead of writing a slew of if then statements, is there a way in VB to write something like

    results.OwnershiInVendor ? "Yes" : "No"

    or what might be the best way to handle this?  There are twelve labels that are returning results from the linq query.

    Thanks


    Brad Allison

    Monday, December 2, 2019 1:15 PM

Answers

  • Either in the database or in code via a Expression.

    This is a mockup

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim dt As New DataTable
    
            ' mock of data from database table
            dt.Columns.Add(New DataColumn() With
                {
                    .ColumnName = "OwnershiInVendor",
                    .DataType = GetType(Boolean),
                    .ColumnMapping = MappingType.Hidden
                })
    
            ' expression
            dt.Columns.Add(New DataColumn() With
                {
                  .ColumnName = "OwnersInVendor",
                  .DataType = GetType(String),
                  .Expression = "IIF(OwnershiInVendor = True, 'Yes','No')"
                })
    
            dt.Rows.Add(New Object() {True})
            dt.Rows.Add(New Object() {False})
            dt.Rows.Add(New Object() {True})
            dt.Rows.Add(New Object() {False})
    
            DataGridView1.DataSource = dt
    
        End Sub
    End Class
    

    SQL

    SELECT
        Id,
        SomeOtherField,
        CASE WHEN OwnershiInVendor = 1 THEN 'Yes' ELSE 'No' END AS YouNameTheAlias
    FROM
        SomeTable


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, December 2, 2019 1:32 PM
    Moderator

All replies

  • Either in the database or in code via a Expression.

    This is a mockup

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim dt As New DataTable
    
            ' mock of data from database table
            dt.Columns.Add(New DataColumn() With
                {
                    .ColumnName = "OwnershiInVendor",
                    .DataType = GetType(Boolean),
                    .ColumnMapping = MappingType.Hidden
                })
    
            ' expression
            dt.Columns.Add(New DataColumn() With
                {
                  .ColumnName = "OwnersInVendor",
                  .DataType = GetType(String),
                  .Expression = "IIF(OwnershiInVendor = True, 'Yes','No')"
                })
    
            dt.Rows.Add(New Object() {True})
            dt.Rows.Add(New Object() {False})
            dt.Rows.Add(New Object() {True})
            dt.Rows.Add(New Object() {False})
    
            DataGridView1.DataSource = dt
    
        End Sub
    End Class
    

    SQL

    SELECT
        Id,
        SomeOtherField,
        CASE WHEN OwnershiInVendor = 1 THEN 'Yes' ELSE 'No' END AS YouNameTheAlias
    FROM
        SomeTable


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, December 2, 2019 1:32 PM
    Moderator
  • Hi

    Is this what you mean?

    		If MessageBox.Show("results.OwnershiInVendor ?", "Choose", MessageBoxButtons.YesNo) = DialogResult.Yes Then
    			' YES chosen
    			Stop
    		Else
    			'NO chosen
    			Stop
    		End If
    


    Regards Les, Livingston, Scotland

    Monday, December 2, 2019 1:36 PM