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