/// <summary> /// Sets route active on web - doesn't do indexing!! /// </summary> /// <param name="id"></param> public static void ActivateRoute(int id) { using (var context = new DataContext()) { var dlo = new System.Data.Linq.DataLoadOptions(); dlo.LoadWith<Route>(x => x.RoutePieces); dlo.LoadWith<Route>(x => x.Type); context.LoadOptions = dlo; var route = context.Routes.FirstOrDefault(x => x.ID == id); if (route != null && route.Status != (int)StatusEnum.ActiveAndIndexed) { // recalc streetcrossing ids foreach (var routePiece in route.RoutePieces) { StreetCrossing crossing = DataHandler.GetCrossing(routePiece.Lat, routePiece.Lng); routePiece.StreetCrossingID = crossing == null ? (int?)null : crossing.ID; } route.Status = (int)StatusEnum.Active; context.SubmitChanges(); } } }
public static string GetPOIName(double lat, double lng) { double e = 0.00000001; using (var context = new DataContext()) { var dlo = new System.Data.Linq.DataLoadOptions(); dlo.LoadWith<RoutePiece>(x => x.Route); dlo.LoadWith<Route>(x => x.Type); context.LoadOptions = dlo; var poi = context.POIs.FirstOrDefault(x => x.Lat > lat - e && x.Lat < lat + e && x.Lng > lng - e && x.Lng < lng + e); if (poi == null) { var piece = context.RoutePieces.FirstOrDefault(x => x.Name != null && x.Lat > lat - e && x.Lat < lat + e && x.Lng > lng - e && x.Lng < lng + e); if (piece != null) { if (piece.Route.Type.HasNamedStationList) { return piece.Route.Type.Name + " " + piece.Name; } else { return piece.Name; } } } else { return poi.Name; } } return null; }
///needs more work - done; public List <CartItem> GetCartItemsByShoppingCartId(int id) { using (var dc = new EcommerceDataContext(_connectionString)) { DataLoadOptions options = new System.Data.Linq.DataLoadOptions(); options.LoadWith <CartItem>(cartItem => cartItem.Product); options.LoadWith <Product>(product => product.Images); dc.LoadOptions = options; return(dc.CartItems.Where(c => c.ShoppingCartId == id).ToList()); } }
public int GetTotalQuantityByShoppingCartId(int id) { using (var dc = new EcommerceDataContext(_connectionString)) { DataLoadOptions options = new System.Data.Linq.DataLoadOptions(); options.LoadWith <CartItem>(cartItem => cartItem.Product); options.LoadWith <Product>(product => product.Images); dc.LoadOptions = options; return(dc.CartItems.AsEnumerable().Where(c => c.ShoppingCartId == id).Sum(c => c.Quantity));//// } }
public static RoutePiece GetRoutePiece(int id) { using (var context = new DataContext()) { var dlo = new System.Data.Linq.DataLoadOptions(); dlo.LoadWith<RoutePiece>(x => x.Route); dlo.LoadWith<Route>(x => x.Type); context.LoadOptions = dlo; return context.RoutePieces.FirstOrDefault(x => x.ID == id); } }
public static List<RoutePiece> GetRoutePieces(int routeID) { using (var context = new DataContext()) { var dlo = new System.Data.Linq.DataLoadOptions(); dlo.LoadWith<RoutePiece>(x => x.Route); dlo.LoadWith<Route>(x => x.Type); context.LoadOptions = dlo; return context.RoutePieces.Where(x => x.RouteID == routeID).OrderBy(x => x.ID).ToList(); } }
public static List<Colonia> GetRouteColonias(int id) { using (var context = new DataContext()) { var dlo = new System.Data.Linq.DataLoadOptions(); dlo.LoadWith<Colonia>(x => x.Delegacion); dlo.LoadWith<Delegacion>(x => x.Estado); context.LoadOptions = dlo; return context.ExecuteQuery<Colonia>("SELECT DISTINCT Colonia.* FROM ((Colonia INNER JOIN Street ON Street.ColoniaID = Colonia.ID) INNER JOIN StreetCrossing ON StreetCrossing.StreetID = Street.ID) INNER JOIN RoutePiece ON RoutePiece.StreetCrossingID = StreetCrossing.ID WHERE RouteID = {0}", id).ToList().OrderBy(x => x.Delegacion.Name).ThenBy(x => x.Name).ToList(); } }
public static List<RoutePiece> GetRoutePiecesTouchingArea(double minLat, double minLng, double maxLat, double maxLng) { using (DataContext context = new DataContext()) { var dlo = new System.Data.Linq.DataLoadOptions(); dlo.LoadWith<RoutePiece>(x => x.Route); context.LoadOptions = dlo; minLat -= ViaDFGraph.M100_IN_DEGREES * 2.5; minLng -= ViaDFGraph.M100_IN_DEGREES * 2.5; maxLat += ViaDFGraph.M100_IN_DEGREES * 2.5; maxLng += ViaDFGraph.M100_IN_DEGREES * 2.5; var pieces = context.RoutePieces.Where(x => x.Lat >= minLat && x.Lat <= maxLat && x.Lng >= minLng && x.Lng <= maxLng).ToList(); var piecesByRoute = pieces.GroupBy(x => x.RouteID).ToList(); var routeIds = piecesByRoute.Select(x => x.Key).ToList(); // add adjacent pieces var idsToLoad = new List<int>(); piecesByRoute.ForEach(p => { idsToLoad.Add(p.Min(x => x.ID) - 1); idsToLoad.Add(p.Max(x => x.ID) + 1); }); pieces.AddRange(context.RoutePieces.Where(x => idsToLoad.Contains(x.ID) && routeIds.Contains(x.RouteID))); return pieces; } }
public static StreetCrossing GetCrossing(double lat, double lng, double maxDistance = ViaDFGraph.M100_IN_DEGREES) { using (var context = new DataContext()) { var dlo = new System.Data.Linq.DataLoadOptions(); dlo.LoadWith<StreetCrossing>(x => x.Street); dlo.LoadWith<StreetCrossing>(x => x.Street1); context.LoadOptions = dlo; StreetCrossing crossing = context.ExecuteQuery<StreetCrossing>("SELECT TOP 1 * FROM StreetCrossing ORDER BY ABS(Lat - {0}) + ABS(Lng - {1}) ", lat, lng).FirstOrDefault(); if (crossing != null && Math.Abs(crossing.Lat - lat) + Math.Abs(crossing.Lng - lng) < maxDistance) { return crossing; } return null; } }
public static List<Route> GetAllRoutesWithPieces(Type type) { using (var context = new viadflib.DataContext()) { var dlo = new System.Data.Linq.DataLoadOptions(); dlo.LoadWith<Route>(x => x.RoutePieces); context.LoadOptions = dlo; return context.Routes.Where(x => x.Status > (int)StatusEnum.New && x.TypeID == type.ID).OrderBy(x => x.ID).ToList(); } }
public static StreetCrossing GetCrossingInMemory(double lat, double lng, double maxDistance = ViaDFGraph.M100_IN_DEGREES) { if (AllCrossings == null) { using (var context = new DataContext()) { var dlo = new System.Data.Linq.DataLoadOptions(); dlo.LoadWith<StreetCrossing>(x => x.Street); dlo.LoadWith<StreetCrossing>(x => x.Street1); context.LoadOptions = dlo; AllCrossings = new DataContext().StreetCrossings.ToList(); } } StreetCrossing crossing = AllCrossings.Aggregate((curmin, x) => (curmin == null || Math.Abs(x.Lat - lat) + Math.Abs(x.Lng - lng) < Math.Abs(curmin.Lat - lat) + Math.Abs(curmin.Lng - lng) ? x : curmin)); if (crossing != null && Math.Abs(crossing.Lat - lat) + Math.Abs(crossing.Lng - lng) < maxDistance) { return crossing; } return null; }
public void DeferedExecutionAndLoadWith() { var db = CreateDB(); DataLoadOptions loadoptions = new DataLoadOptions(); loadoptions.LoadWith <Customer>(c => c.Orders); db.LoadOptions = loadoptions; var customer = db.Customers.First(); Assert.IsFalse(customer.Orders.IsDeferred, "#1"); Assert.IsTrue(customer.Orders.HasLoadedOrAssignedValues, "#2"); }
public static List<Route> GetConnectionRoutes(double lat, double lng, double maxDistanceKm, int excludeRouteId = 0) { double maxDistanceDegrees = maxDistanceKm * ViaDFGraph.KM_IN_DEGREES; using (DataContext context = new DataContext()) { var dlo = new System.Data.Linq.DataLoadOptions(); dlo.LoadWith<Route>(x => x.Type); context.LoadOptions = dlo; List<Route> routes = context.RoutePieces.Where(x => x.RouteID != excludeRouteId && Math.Abs(x.Lat - lat) + Math.Abs(x.Lng - lng) < maxDistanceDegrees).OrderBy(x => Math.Abs(x.Lat - lat) + Math.Abs(x.Lng - lng)).Select(x => x.Route).Distinct().ToList(); return routes; } }
public static List<Route> GetAllRoutesForIds(List<int> routeIds) { if (routeIds.Count == 0) { return new List<Route>(); } using (var context = new viadflib.DataContext()) { var dlo = new System.Data.Linq.DataLoadOptions(); dlo.LoadWith<Route>(x => x.Type); context.LoadOptions = dlo; return context.Routes.Where(x => routeIds.Contains(x.ID)).ToList(); } }
public static Colonia GetColoniaAtPosition(double lat, double lng) { var streetCrossing = GetCrossing(lat, lng); if (streetCrossing != null) { using (var context = new DataContext()) { var dlo = new System.Data.Linq.DataLoadOptions(); dlo.LoadWith<Colonia>(x => x.Delegacion); context.LoadOptions = dlo; return context.ExecuteQuery<Colonia>("SELECT TOP 1 Colonia.* FROM ((Colonia INNER JOIN Street ON Street.ColoniaID = Colonia.ID) INNER JOIN StreetCrossing ON StreetCrossing.StreetID = Street.ID) WHERE StreetCrossing.ID = {0}", streetCrossing.ID).FirstOrDefault(); } } return null; }
public static List<List<Route>> FindDuplicates(string email = null) { List<List<Route>> lists = new List<List<Route>>(); using (var context = new DataContext()) { var dlo = new System.Data.Linq.DataLoadOptions(); dlo.LoadWith<Route>(x => x.RoutePieces); context.LoadOptions = dlo; List<Route> allRoutes = context.Routes.ToList(); foreach (Route route in allRoutes.Where(x => x.Email == email || email == null)) { if (!lists.Any(x => x.Any(y => y.ID == route.ID))) { List<Route> duplicates = new List<Route>(); duplicates.Add(route); foreach (Route route2 in allRoutes) { if (route.ID != route2.ID && route.TypeID == route2.TypeID) { double similarity = CalculateSimilarity(route, route2); if (similarity > 0.9) { duplicates.Add(route2); } } } if (duplicates.Count > 1) { lists.Add(duplicates); } } } } return lists; }
public void DeferedExecutionAndLoadWith() { var db = CreateDB(); DataLoadOptions loadoptions = new DataLoadOptions(); loadoptions.LoadWith<Customer>(c => c.Orders); db.LoadOptions = loadoptions; var customer = db.Customers.First(); Assert.IsFalse(customer.Orders.IsDeferred, "#1"); Assert.IsTrue(customer.Orders.HasLoadedOrAssignedValues, "#2"); }
public static List<string> GetRouteStreets(int id) { List<string> result = new List<string>(); using (var context = new DataContext()) { var dlo = new System.Data.Linq.DataLoadOptions(); dlo.LoadWith<RoutePiece>(x => x.StreetCrossing); dlo.LoadWith<StreetCrossing>(x => x.Street); dlo.LoadWith<StreetCrossing>(x => x.Street1); context.LoadOptions = dlo; Street lastStreet1 = null; Street lastStreet2 = null; int lastRoutePieceId = 0; int lastValidRoutePieceId = 0; Street sameStreet = null; int sameCount = 0; int sameStartRoutePieceID = 0; //deep load pieces with street information List<RoutePiece> pieces = context.RoutePieces.Where(x => x.RouteID == id).OrderBy(x => x.ID).ToList(); foreach (RoutePiece piece in pieces) { var crossing = piece.StreetCrossing; if (crossing != null) { Street street1 = crossing.Street; Street street2 = crossing.Street1; if (lastStreet1 != null && lastStreet2 != null) { Street newSameStreet = null; if (street1.Name == lastStreet1.Name || street1.Name == lastStreet2.Name) { newSameStreet = street1; } else if (street2.Name == lastStreet1.Name || street2.Name == lastStreet2.Name) { newSameStreet = street2; } if (sameStreet != null && newSameStreet != null && newSameStreet.Name == sameStreet.Name) { sameCount++; lastValidRoutePieceId = piece.ID; } else { if (newSameStreet != null) { if (sameCount >= 3) { //RouteStreetIndex index = new RouteStreetIndex(); //index.RouteID = id; //index.StreetID = sameStreet.ID; //index.FromRoutePieceID = sameStartRoutePieceID; //index.ToRoutePieceID = lastValidRoutePieceId; if (result.Count == 0 || result.Last() != sameStreet.Name) { result.Add(sameStreet.Name); } } sameStartRoutePieceID = lastRoutePieceId; lastValidRoutePieceId = 0; sameStreet = newSameStreet; sameCount = 2; } } } lastStreet1 = street1; lastStreet2 = street2; lastRoutePieceId = piece.ID; } } if (sameCount >= 3) { result.Add(sameStreet.Name); } } return result; }