Freigeben über


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").

Comments

  • Anonymous
    November 12, 2007
    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> ?

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

  • Anonymous
    November 21, 2007
    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.

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

  • Anonymous
    May 19, 2008
    Dinesh Kularni , who was formerly on the LINQ to SQL team and is now on the Silverlight team, has been

  • Anonymous
    May 19, 2008
    Dinesh Kularni a publié depuis novembre 5 astuces sur LINQ To SQL : LINQ to SQL Tips 1: how to map an

  • Anonymous
    July 07, 2008
    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

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