public static DhlZone LoadSingle(DhlZones key)
 {
     DhlZone.LoadAll();
     return(DhlZone.cache.FirstOrDefault(runner => runner.Key == key));
 }
        public static DhlZone LoadSingle(String key)
        {
            var enumKey = (DhlZones)Enum.Parse(typeof(DhlZones), key);

            return(DhlZone.LoadSingle(enumKey));
        }
        //DHL new
        #region CreateDHL
        public static Byte[] CreateDHL()
        {
            MemoryStream stream = null;
            StreamWriter writer = null;

            try
            {
                //var originalEntity = MyDataContext.Default.Mailings
                //	.Where(runner => runner.DateOfShipping == null)
                //	.Where(runner => runner.ShippingMethod == ShippingMethods.DHL)
                //	.ToList()
                //	.Where(runner => runner.Sales.AreAllBilled())
                //	.Where(runner => runner.Sales.Count() > 0)
                //	.Where(runner => runner.Sales.First().Invoice != null)
                //	.Where(runner => MyDataContext.Default.MailingCostCountries.Any(x => x.IsoCode2 == runner.RecepientCountry))
                //	.ToList();

                var originalEntity = MyDataContext.Default.Mailings
                                     .Where(runner => runner.DateOfShipping == null)
                                     .Where(runner => runner.ShippingMethod == ShippingMethods.DHL)
                                     .ToList()
                                     .Where(runner => runner.Sales.AreAllBilled())
                                     .Where(runner => MailingCostCountry.LoadByName(runner.RecepientCountry) != null);

                stream = new MemoryStream();
                writer = new StreamWriter(stream, System.Text.Encoding.GetEncoding(1252));

                writer.Write("Sender Name 1" + ";");
                writer.Write("Sender Street" + ";");
                writer.Write("Sender Nr" + ";");
                writer.Write("Sender Postcode" + ";");
                writer.Write("Sender City" + ";");
                writer.Write("Sender Country" + ";");
                writer.Write("Sender E-Mail-Addresse" + ";");
                writer.Write("Sender Phone" + ";");

                writer.Write("Recepient Name 1" + ";");
                writer.Write("Recepient Name 2 / Postnr" + ";");
                writer.Write("Recepient Street" + ";");
                writer.Write("Recepient Nr" + ";");
                writer.Write("Recepient Address 1" + ";");
                writer.Write("Recepient Postcode" + ";");
                writer.Write("Recepient City" + ";");
                writer.Write("Recepient Country" + ";");
                writer.Write("Recepient E-Mail-Addresse" + ";");
                writer.Write("Recepient Phone" + ";");

                writer.Write("Transactionnr." + ";");
                writer.Write("Product- und Servicedetails" + ";");
                writer.Write("Weight" + ";");
                writer.Write(Environment.NewLine);

                foreach (Mailing mailing in originalEntity)
                {
                    var country = MailingCostCountry.LoadByName(mailing.RecepientCountry);
                    var dhlZone = DhlZone.LoadSingle(country.DhlProductCode);
                    var sale    = mailing.Sales.First();

                    //Absenderadresse
                    writer.Write(ShopperConfiguration.Default.Mandantor.Company + ";");
                    writer.Write(ShopperConfiguration.Default.Mandantor.Street + ";");
                    writer.Write(ShopperConfiguration.Default.Mandantor.StreetNr + ";");
                    writer.Write(ShopperConfiguration.Default.Mandantor.Zip + ";");
                    writer.Write(ShopperConfiguration.Default.Mandantor.City + ";");
                    writer.Write(ShopperConfiguration.Default.Mandantor.CountryIso3 + ";");
                    writer.Write(ShopperConfiguration.Default.Mandantor.Email + ";");
                    writer.Write(ShopperConfiguration.Default.Mandantor.Phone + ";");

                    //Empfängeradresse
                    Int32  recepientLength = mailing.RecepientName.Length > 30 ? 30 : mailing.RecepientName.Length;
                    String recepientName   = mailing.RecepientName.Substring(0, recepientLength);
                    String streetNumber    = CsvExporter.IntrashipGetStreetNumber(mailing.RecepientStreet1);
                    streetNumber = String.IsNullOrWhiteSpace(streetNumber) ? "_" : streetNumber;

                    writer.Write(recepientName + ";");                                                                                            //Name 1 (Empfänger)
                    writer.Write(mailing.IsDeliveredToPackstation ? mailing.GetPackstationUser() : "" + ";");                                     //Name 2/Postnummer (Empfänger)
                    writer.Write(mailing.IsDeliveredToPackstation ? "" : CsvExporter.IntrashipGetStreetNameOnly(mailing.RecepientStreet1) + ";"); //Straße (Empfänger)
                    writer.Write(mailing.IsDeliveredToPackstation ? "" : streetNumber + ";");                                                     //Hausnummer (Empfänger)
                    writer.Write(mailing.IsDeliveredToPackstation ? "" : mailing.RecepientStreet2 + ";");                                         //Adresszusatz 1 (Empfänger)
                    writer.Write(mailing.RecepientPostcode + ";");                                                                                //PLZ (Empfänger)
                    writer.Write(mailing.RecepientCity + ";");                                                                                    //Ort (Empfänger)
                    writer.Write(MailingCostCountry.LoadByName(mailing.RecepientCountry)?.IsoCode3 + ";");                                        //Land (Empfänger)
                    writer.Write(mailing.RecepientEmail + ";");                                                                                   //Email-Adresse (Empfänger)
                    writer.Write(mailing.RecepientPhone + ";");                                                                                   //Telefonnummer (Empfänger)

                    writer.Write(dhlZone.DhlProduct + ";");                                                                                       //Abrechnugnsnummer
                    writer.Write(dhlZone.Dhl + ";");                                                                                              //Produkt- und Servicedetails
                    writer.Write(mailing.TotalWeight / 1000 + ";");                                                                               //Gewicht

                    writer.Write(Environment.NewLine);
                }
            }
            finally
            {
                writer.Flush();
                stream.Position = 0;
            }

            Byte[] result = new Byte[stream.Length];
            stream.Position = 0;
            stream.Read(result, 0, result.Length);
            stream.Close();
            return(result);
        }