aspx Tutorial

.NET Articles,jQuery demo, asp.net with jQuery, online tutorial,Jquery, SilverLight, Javascript, asp.net,JSON, MVC,.NET Articles,demo, Web Services, .NET articles, Sharepoint 2010, visual studio 2010,Aamir Hasan,IT
Advertise Here

Advertize

wwwSW
Posted by Aamir Hasan   on Tuesday, June 7, 2011 Total Views:  

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

Protected by Copyscape Online Plagiarism Tool

Comments (15) -

rihanna tickets
rihanna tickets United States
6/21/2011 6:34:38 PM #

In fact your inventive writing skills has inspired me to begin my own Blog engine blog now.

penis enlargement
penis enlargement United States
10/24/2011 5:32:22 AM #

great post.. i really enjoyed it

barbour ashby
barbour ashby People's Republic of China
12/2/2011 4:29:56 PM #

There are many brands of barbour Shoreman Jacket in the market today. Each of the brand promises to bring out something new to the customers. barbour fusilier is also a renowned and reputed brand in the market. This brand is in fact reputed for the jackets barbour . You can purchase a Barbour enduro leather jacket and you would be amazed to find yourself. There are different varieties in the barbour jacket that you would get in the market.We will try to provide you few varieties of the barbour scott bracken jacket that are available in the market. The barbour jackets is basically a classically designed and styled Barbour combat coat. http://www.uk-barbourjacket.com/

UGG Boots
UGG Boots People's Republic of China
12/4/2011 5:02:59 PM #

555yxj9
http://www.cheapestuggsbootssale.org/

Air Max 2011
Air Max 2011 People's Republic of China
12/4/2011 10:24:53 PM #

555ce1
Don't wait for opportunity, and to create opportunities.

Air Max 2011
Air Max 2011 South Africa
12/4/2011 10:25:05 PM #

555ce1
Don't wait for opportunity, and to create opportunities.

Cheap Air Max
Cheap Air Max People's Republic of China
12/4/2011 10:39:52 PM #

555yxj21
I say little but I think the more,in wine there is truth.
http://www.airmaxforcheaponline.com/

Asics Running Shoes
Asics Running Shoes United States
12/4/2011 10:58:33 PM #

555ce3
http://www.asicshoes4you.com/

UGG Boots Sale
UGG Boots Sale People's Republic of China
12/4/2011 11:05:50 PM #

555yxj27
In moderating, not satisfying, desires, lies peace.
http://www.uggkensingtonbootssale.info/

ugg boots online
ugg boots online United States
12/5/2011 2:25:47 AM #

555ce25
http://www.wholesaleuggsbootses.org

supra footwear
supra footwear People's Republic of China
12/6/2011 3:10:46 AM #

The arrangement <A title="cheap supra skytop" href="http://www.tksocietysupra.com">cheap supra skytop</A> comes as Comcast moves to fulfill its<A title="new air jordan 2011" href="http://www.newairjordan2011.com">new air jordan 2011</A> commitment to federal regulators to strengthen local, public-interest programming in<A title="cheap jordan shoes" href="http://www.blackjordan.com">cheap jordan shoes</A> the wake of its purchase of NBCUniversal earlier<A title="jordan slipper" href="http://www.newairjordan2014.com">jordan slipper</A>this year.The partnerships also continue the trend<A title="supra footwear" href="http://www.tksocietysupra.com">supra footwear</A> toward content sharing throughout the media industry<A title="discount ugg boots" href="http://www.classicardyuggs.com">discount ugg boots</A> as operators try to trim the high costs that come with producing stories on their own.<A title="cheap ugg boots uk"

Mulberry Alexa Bag Sale
Mulberry Alexa Bag Sale Portugal
12/9/2011 9:18:26 PM #

The Mulberry Bags Sale keep your like feeling in 2011 . If you have the fashion mulberry bag sales , you will can't help falling in love with http://www.mulberry-bag-uk.org/  ,confidence that mulberry bags bring to you . As for a world famous brand , Mulberry Bags is keep moving in the fashion world , continues to launch more and more fashion Mulberry Alexa Bag to meet more people's fashion feel. Many people look for a certain option with mulberry handbags , which are colors, owning a black Mulberry Alexa Bags increase the sense of versatility, I think you will like the mulberry clutch .,http://www.mulberry-bag-sales.net/  uk.

fddf
fddf People's Republic of China
3/31/2012 9:26:39 PM #

http://www.louisvuittonoutletstoresusa.com

fafafa
fafafa Slovenia
4/23/2012 2:37:56 AM #

http://www.christianlouboutinukk.org   christian louboutin
http://www.tiffanyuks.org   tiffany uk
http://www.gucciukbeltuk.org   gucci belt
http://www.frsaclouisvuittonsac.com   louis vuitton sac
http://www.chanelukoutletuks.org   chanel outlet
http://www.burberryukoutletuk.org   burberry
http://www.poloralphlaurenuko.org   ralph lauren uk

Pingbacks and trackbacks (2)+

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Advertizement 1
Advertizement 2
Advertizement 3
Advertizement 4
Advertizement 5