private static bool UpdateH2HRecord(ref House2HouseRecordData rec)
        {
            if (rec.ItemId < 0)
            {
                return(AddH2HRecord(ref rec));
            }
            try {
                using (var db = new House2HouseRecordDataContext()) {
                    int itemId = rec.ItemId;
                    var rec2   = db.House2HouseRecordItems.Single(s => s.ItemId == itemId);

                    rec2.Date                 = rec.Date;
                    rec2.HouseAptNumber       = rec.HouseAptNumber;
                    rec2.NamePlacementRemarks = rec.NamePlacementRemarks;
                    rec2.StreetItemId         = rec.StreetItemId;
                    rec2.Symbol               = rec.Symbol;
                    rec2.TerritoryItemId      = rec.TerritoryItemId;

                    db.SubmitChanges();

                    return(true);
                }
            } catch (InvalidOperationException) {
                return(AddH2HRecord(ref rec)); //rv not found, lets create it`
            }
        }
 public static void CheckDatabase()
 {
     using (var db = new House2HouseRecordDataContext()) {
         if (db.DatabaseExists() == false)
         {
             db.CreateDatabase();
         }
     }
 }
 public static House2HouseRecordData GetHouse2HouseRecord(int itemId)
 {
     using (var db = new House2HouseRecordDataContext()) {
         try {
             var r = db.House2HouseRecordItems.Single(s => s.ItemId == itemId);
             return(House2HouseRecordData.Copy(r));
         } catch {
             return(new House2HouseRecordData());
         }
     }
 }
        public static House2HouseRecordData[] GetHouse2HouseRecords(SortOrder so, int maxReturnCount = 25)
        {
            var records = new List <House2HouseRecordData>();

            using (var db = new House2HouseRecordDataContext()) {
                try {
                    if (maxReturnCount == -1)
                    {
                        maxReturnCount = db.House2HouseRecordItems.Count();
                    }
                    IEnumerable <House2HouseRecordDataItem> demRecs = null;
                    IQueryable <House2HouseRecordDataItem>  q;
                    if (so == SortOrder.DateNewestToOldest || so == SortOrder.DateOldestToNewest)
                    {
                        q = (from x in db.House2HouseRecordItems
                             orderby x.Date
                             select x).Take(maxReturnCount);
                        demRecs = so == SortOrder.DateNewestToOldest ? q.ToArray().Reverse() : q.ToArray();
                    }
                    else if (so == SortOrder.StreetAToZ || so == SortOrder.StreetZToA)
                    {
                        q = (from x in db.House2HouseRecordItems
                             orderby StreetBuildingInterface.GetStreetName(x.StreetItemId)
                             select x).Take(maxReturnCount);
                        demRecs = so == SortOrder.StreetZToA ? q.ToArray().Reverse() : q.ToArray();
                    }
                    else if (so == SortOrder.AddressNumberHighToLow || so == SortOrder.AddressNumberLowToHigh)
                    {
                        q = (from x in db.House2HouseRecordItems
                             orderby x.HouseAptNumber
                             select x).Take(maxReturnCount);
                        demRecs = so == SortOrder.AddressNumberHighToLow ? q.ToArray().Reverse() : q.ToArray();
                    }
                    if (demRecs != null)
                    {
                        records.AddRange(demRecs.Select(House2HouseRecordData.Copy));
                    }
                    return(records.ToArray());
                } catch {
                    return(records.ToArray());
                }
            }
        }
 private static bool AddH2HRecord(ref House2HouseRecordData rec)
 {
     using (var db = new House2HouseRecordDataContext()) {
         var r   = House2HouseRecordData.Copy(rec);
         var qry = from x in db.House2HouseRecordItems
                   //unique items
                   where x.HouseAptNumber.Equals(r.HouseAptNumber) &&
                   x.StreetItemId.Equals(r.StreetItemId) &&
                   x.TerritoryItemId.Equals(r.TerritoryItemId)
                   select x;
         if (qry.Any())
         {
             throw new H2HRecordAlreadyExistsException();
         }
         db.House2HouseRecordItems.InsertOnSubmit(r);
         db.SubmitChanges();
         rec.ItemId = r.ItemId;
         return(rec.ItemId >= 0);
     }
 }