LINQ to SQL Tips 1: how to map an enum

I was out on vacation (Zion, Bryce Canyon and Grand Canyon National Parks) so my blog went dark for a while. Also, I noticed a few comments that were incorrectly flagged by the spam filter - I have now published them, albeit after a delay. Sorry about that delay.

As we get closer to a release, it is time to mention a few less known LINQ to SQL (fka DLinq) features that I get emails about. Here is the first in the series about enum mapping. Here is an example based on the northwind database that enhances the usual generated Northwind DataContext and entity classes.

    public enum ShippingCompany {

        Undefined,

        FedEx,

        UPS,

        DHL

    }

 

    // A mini Order class with hand-mapping

    [Table(Name="Orders")]

    class EnumOrder

    {

        [Column(IsPrimaryKey=true)]

        public int OrderID;        

        [Column]

        public string CustomerID;

        [Column]

        public ShippingCompany ShipVia;

 

    }

 

    class OrdProj

    {

        public int OrderID;

        public string CustomerID;

        public int? ShipVia;

    }

 

    class NewNW: NorthwindDataContext

    {

        public NewNW(): base() {}

 

        public Table<EnumOrder> EnumOrders;

    }

 

    class Program

    { 

        static void Main(string[] args)

        {

            //NorthwindDataContext db = new NorthwindDataContext();

            //db.Log = Console.Out;

           

 

            NewNW db2 = new NewNW();

            db2.Log = Console.Out;

 

            var q = from o in db2.EnumOrders

                     where o.CustomerID == "ALFKI"

                     select o;

 

 

            var q2 = (from o in db2.Orders

                     where o.CustomerID == "ALFKI"

                     select new OrdProj { OrderID = o.OrderID, CustomerID = o.CustomerID, ShipVia = o.ShipVia }).Distinct();

 

            ObjectDumper.Write(q);

        }

    }

The output is:

SELECT [t0].[OrderID], [t0].[CustomerID], [t0].[ShipVia]

FROM [Orders] AS [t0]

WHERE [t0].[CustomerID] = @p0

-- @p0: Input NVarChar (Size = 5; Prec = 0; Scale = 0) [ALFKI]

-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.21022.7

 

OrderID=10643   CustomerID=ALFKI        ShipVia=FedEx

OrderID=10692   CustomerID=ALFKI        ShipVia=UPS

OrderID=10702   CustomerID=ALFKI        ShipVia=FedEx

OrderID=10835   CustomerID=ALFKI        ShipVia=DHL

OrderID=10952   CustomerID=ALFKI        ShipVia=FedEx

OrderID=11011   CustomerID=ALFKI        ShipVia=FedEx

Press any key to continue . . .

You can map an enum to integral type as in this case or to a string if the strings match the enum values (e.g. "FedEx").

 

Published 05 November 07 09:45 by Dinesh.Kulkarni

Comments

# Howard said on November 12, 2007 12:26 PM:

ShipVia is a nullable column, presumably we get an exception when trying to map a dbNULL to an enum - or could we define EnumOrders.ShipVia as nullable<enum> ?

# Charlie Calvert's Community Blog said on November 19, 2007 6:04 PM:

Welcome to the thirty-sixth issue of Community Convergence. This is the big day, with Visual Studio 2008

# Spence said on November 21, 2007 11:28 PM:

If it were that easy to do, why not allow you to specify enums in the designer and then have the type safe enums propagate into all your code right from the data layer.

# Charlie Calvert's Community Blog said on December 10, 2007 3:09 AM:

Welcome to the thirty-sixth issue of Community Convergence. This is the big day, with Visual Studio 2008

# Hot Topics said on May 19, 2008 3:35 PM:

Dinesh Kularni , who was formerly on the LINQ to SQL team and is now on the Silverlight team, has been

# Matthieu MEZIL said on May 20, 2008 4:24 AM:

Dinesh Kularni a publié depuis novembre 5 astuces sur LINQ To SQL : LINQ to SQL Tips 1: how to map an

# LaptopHeaven: blogging that bytes said on July 7, 2008 9:05 AM:

I found a series of LINQ to SQL tips over at Dinesh's Cyberstation . LINQ to SQL Tips 1: how to map an enum LINQ to SQL Tips 2: how to use common base class for all entities LINQ to SQL Tips 3: Deferred (lazy) or eager loading of related objects with

# Iga lahendus tekitab uusi probleeme ehk alati võib leida veel ühe bugi. said on March 16, 2009 5:59 PM:

Üks teemaisd, mis mind mõnda aega on Linq to SQL juures mõtisklema pannud, on selle lifetime ehk kui

New Comments to this post are disabled

About Dinesh.Kulkarni

I am a program manager in the Visual C# Product Unit of Microsoft. I am currently working on the LINQ project with specific responsibility for DLinq. Previously, I have been in a PM in SQL Server working on ObjectSpaces and DataSet. In pre-MS life, I have worked for companies ranging from startup to IBM on a wide range of software projects. Before I started working, I did M.S.E.E. and Ph.D. (CSE) from the University of Notre Dame and B.Tech. E.E. from IIT Bombay, India.

Search

This Blog

Syndication

Page view tracker