In this tutorial you will see if result is "F" we will convert it into "Female" and if result is "M" we will convert it into "Male" and other cases we will convert into "Other" using Linq case statement.
Here’s an example
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Generating a Case Statement in LINQ to SQL | aspxtutorial.com</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server"
CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
</div>
</form>
</body>
</html>
C#
var query = from pat in Display()
select new
{
Name = pat.Name,
Gender = (
pat.Gender == "M" ? "Male" :
pat.Gender == "F" ? "Female" :
"Other"
)
};
GridView1.DataSource = query;
GridView1.DataBind();
}
public List<Patient> Display()
{
List<Patient> Obj = new List<Patient>();
Obj.Add(new Patient() { Name = "Aamir Hasan", Gender = "M" });
Obj.Add(new Patient() { Name = "Mahwish Akhtar", Gender = "F" });
Obj.Add(new Patient() { Name = "Saba Khan", Gender = "F" });
Obj.Add(new Patient() { Name = "John", Gender = "M" });
Obj.Add(new Patient() { Name = "Fasial ", Gender = "M" });
return Obj;
}
public class Patient
{
public String Name { get; set; }
public String Gender { get; set; }
}
VB.NET
Protected Sub Page_Load(sender As Object, e As EventArgs)
Dim que = From pat In Display() Select New With {Key .Name = pat.Name, Key .Gender = If(pat.Gender = "F", "Female", If(pat.Gender = "M", "Female", "Other"))}
GridView1.DataSource = que
GridView1.DataBind()
End Sub
Public Function Display() As List(Of Patient)
Dim Obj As New List(Of Patient)()
Obj.Add(New Patient() With { _
.Name = "Aamir Hasan", _
.Gender = "M" _
})
Obj.Add(New Patient() With { _
.Name = "Mahwish Akhtar", _
.Gender = "F" _
})
Obj.Add(New Patient() With { _
.Name = "Saba Khan", _
.Gender = "F" _
})
Obj.Add(New Patient() With { _
.Name = "John", _
.Gender = "M" _
})
Obj.Add(New Patient() With { _
.Name = "Fasial ", _
.Gender = "M" _
})
Return Obj
End Function
Public Class Patient
Public Property Name() As [String]
Get
Return m_Name
End Get
Set(value As [String])
m_Name = value
End Set
End Property
Private m_Name As [String]
Public Property Gender() As [String]
Get
Return m_Gender
End Get
Set(value As [String])
m_Gender = value
End Set
End Property
Private m_Gender As [String]
End Class
Output

Download
Case-Statement-in-LinqCsharp.rar (1.15 kb)
Case-Statement-in-LinqVB.rar (1.02 kb)
See live demo