
USE ExampleDatabase; GO -- Create table CREATE TABLE Boundaries_Country( FeatureID UNIQUEIDENTIFIER NOT NULL PRIMARY KEY , CountryName VARCHAR (100) NOT NULL UNIQUE , CountryBoundary GEOGRAPHY NOT NULL ) CREATE SPATIAL INDEX SpatialIndex ON Boundaries_Country (CountryBoundary); GO * This source code was highlighted with Source Code Highlighter .
USE ExampleDatabase; GO -- Create table CREATE TABLE Boundaries_Country( FeatureID UNIQUEIDENTIFIER NOT NULL PRIMARY KEY , CountryName VARCHAR (100) NOT NULL UNIQUE , CountryBoundary GEOGRAPHY NOT NULL ) CREATE SPATIAL INDEX SpatialIndex ON Boundaries_Country (CountryBoundary); GO * This source code was highlighted with Source Code Highlighter .USE ExampleDatabase; GO -- Create table CREATE TABLE Boundaries_Country( FeatureID UNIQUEIDENTIFIER NOT NULL PRIMARY KEY , CountryName VARCHAR (100) NOT NULL UNIQUE , CountryBoundary GEOGRAPHY NOT NULL ) CREATE SPATIAL INDEX SpatialIndex ON Boundaries_Country (CountryBoundary); GO * This source code was highlighted with Source Code Highlighter .USE ExampleDatabase; GO -- Create table CREATE TABLE Boundaries_Country( FeatureID UNIQUEIDENTIFIER NOT NULL PRIMARY KEY , CountryName VARCHAR (100) NOT NULL UNIQUE , CountryBoundary GEOGRAPHY NOT NULL ) CREATE SPATIAL INDEX SpatialIndex ON Boundaries_Country (CountryBoundary); GO * This source code was highlighted with Source Code Highlighter .USE ExampleDatabase; GO -- Create table CREATE TABLE Boundaries_Country( FeatureID UNIQUEIDENTIFIER NOT NULL PRIMARY KEY , CountryName VARCHAR (100) NOT NULL UNIQUE , CountryBoundary GEOGRAPHY NOT NULL ) CREATE SPATIAL INDEX SpatialIndex ON Boundaries_Country (CountryBoundary); GO * This source code was highlighted with Source Code Highlighter .USE ExampleDatabase; GO -- Create table CREATE TABLE Boundaries_Country( FeatureID UNIQUEIDENTIFIER NOT NULL PRIMARY KEY , CountryName VARCHAR (100) NOT NULL UNIQUE , CountryBoundary GEOGRAPHY NOT NULL ) CREATE SPATIAL INDEX SpatialIndex ON Boundaries_Country (CountryBoundary); GO * This source code was highlighted with Source Code Highlighter .USE ExampleDatabase; GO -- Create table CREATE TABLE Boundaries_Country( FeatureID UNIQUEIDENTIFIER NOT NULL PRIMARY KEY , CountryName VARCHAR (100) NOT NULL UNIQUE , CountryBoundary GEOGRAPHY NOT NULL ) CREATE SPATIAL INDEX SpatialIndex ON Boundaries_Country (CountryBoundary); GO * This source code was highlighted with Source Code Highlighter .USE ExampleDatabase; GO -- Create table CREATE TABLE Boundaries_Country( FeatureID UNIQUEIDENTIFIER NOT NULL PRIMARY KEY , CountryName VARCHAR (100) NOT NULL UNIQUE , CountryBoundary GEOGRAPHY NOT NULL ) CREATE SPATIAL INDEX SpatialIndex ON Boundaries_Country (CountryBoundary); GO * This source code was highlighted with Source Code Highlighter .USE ExampleDatabase; GO -- Create table CREATE TABLE Boundaries_Country( FeatureID UNIQUEIDENTIFIER NOT NULL PRIMARY KEY , CountryName VARCHAR (100) NOT NULL UNIQUE , CountryBoundary GEOGRAPHY NOT NULL ) CREATE SPATIAL INDEX SpatialIndex ON Boundaries_Country (CountryBoundary); GO * This source code was highlighted with Source Code Highlighter .USE ExampleDatabase; GO -- Create table CREATE TABLE Boundaries_Country( FeatureID UNIQUEIDENTIFIER NOT NULL PRIMARY KEY , CountryName VARCHAR (100) NOT NULL UNIQUE , CountryBoundary GEOGRAPHY NOT NULL ) CREATE SPATIAL INDEX SpatialIndex ON Boundaries_Country (CountryBoundary); GO * This source code was highlighted with Source Code Highlighter .USE ExampleDatabase; GO -- Create table CREATE TABLE Boundaries_Country( FeatureID UNIQUEIDENTIFIER NOT NULL PRIMARY KEY , CountryName VARCHAR (100) NOT NULL UNIQUE , CountryBoundary GEOGRAPHY NOT NULL ) CREATE SPATIAL INDEX SpatialIndex ON Boundaries_Country (CountryBoundary); GO * This source code was highlighted with Source Code Highlighter .USE ExampleDatabase; GO -- Create table CREATE TABLE Boundaries_Country( FeatureID UNIQUEIDENTIFIER NOT NULL PRIMARY KEY , CountryName VARCHAR (100) NOT NULL UNIQUE , CountryBoundary GEOGRAPHY NOT NULL ) CREATE SPATIAL INDEX SpatialIndex ON Boundaries_Country (CountryBoundary); GO * This source code was highlighted with Source Code Highlighter .USE ExampleDatabase; GO -- Create table CREATE TABLE Boundaries_Country( FeatureID UNIQUEIDENTIFIER NOT NULL PRIMARY KEY , CountryName VARCHAR (100) NOT NULL UNIQUE , CountryBoundary GEOGRAPHY NOT NULL ) CREATE SPATIAL INDEX SpatialIndex ON Boundaries_Country (CountryBoundary); GO * This source code was highlighted with Source Code Highlighter .

* This source code was highlighted with Source Code Highlighter .
- using System;
- using System.Data.Linq.Mapping;
- using Microsoft.SqlServer.Types;
- namespace mynamespace
- {
- [Table ()]
- public sealed class Boundaries_Country
- {
- [Column (AutoSync = AutoSync.OnInsert, DbType = "uniqueidentifier" , IsPrimaryKey = true , IsDbGenerated = true , UpdateCheck = UpdateCheck.Never)]
- public Guid FeatureID;
- [Column (DbType = "varchar (100)" , CanBeNull = false )]
- public string CountryName;
- [Column ( / * DbType = "geography", * / CanBeNull = false )]
- public SqlGeography CountryBoundary;
- }
- }
* This source code was highlighted with Source Code Highlighter .
- using System.Data.Linq;
- namespace mynamespace
- {
- public class ExampleDatabase: DataContext
- {
- public table <Boundaries_Country> BoundariesCountry;
- public ExampleDatabase ( string connectionString)
- : base (connectionString)
- {
- }
- }
- }
* This source code was highlighted with Source Code Highlighter .
- static void Main ( string [] args)
- {
- ExampleDatabase db = new ExampleDatabase ( @ "..." );
- var q = from item in db.BoundariesCountry
- where item.CountryName.StartsWith ( "C" )
- select item;
- foreach ( var item in q)
- Console .WriteLine (item.CountryName);
- }
One interesting point. If, in debug mode, stop the execution of the program on line 9 and view the contents of the variable q, then we will see a LINQ to SQL generated query.
* This source code was highlighted with Source Code Highlighter .
- var q = from item in db.BoundariesCountry
- where item.CountryName.StartsWith ( "C" ) &&
- item.CountryBoundary.STIntersects (sqlEnvelope) .Value
- select item;
- foreach ( var item in q)
- Console .WriteLine (item.CountryName);
* This source code was highlighted with Source Code Highlighter .
- CREATE PROCEDURE [dbo]. [Sp_bbx_Boundaries_Country]
- @boundingBox varbinary ( max )
- AS
- BEGIN
- SET NOCOUNT ON ;
- SELECT *
- FROM dbo.Boundaries_Country
- WHERE GEOGRAPHY :: STGeomFromWKB (@boundingBox,
- 4326) .STIntersects (CountryBoundary) = 1;
- RETURN ;
- END
* This source code was highlighted with Source Code Highlighter .
- [Function ()]
- public ISingleResult <Boundaries_Country> sp_bbx_Boundaries_Country (
- [Parameter (DbType = "varbinary (max)" )] byte [] boundingBox)
- {
- IExecuteResult execResult = this .ExecuteMethodCall ( this , ((MethodInfo)
- (MethodInfo.GetCurrentMethod ())), boundingBox);
- ISingleResult <Boundaries_Country> result =
- ((ISingleResult <Boundaries_Country>) execResult.ReturnValue);
- return result;
- }
* This source code was highlighted with Source Code Highlighter .
- var q = from item in db.sp_bbx_Boundaries_Country (
- sqlEnvelope.STAsBinary (). Buffer)
- where item.CountryName.StartsWith ( "C" )
- select item;
- foreach ( var item in q)
- Console .WriteLine (item.CountryName);
Note, if you, as in my working draft, have several tables with geographic data, then the following options are available.* This source code was highlighted with Source Code Highlighter .
- CREATE FUNCTION [dbo]. [F_bbx_Boundaries_Country]
- (
- @boundingBox varbinary ( max )
- )
- RETURNS TABLE
- AS
- RETURN
- (
- SELECT *
- FROM dbo.Boundaries_Country
- WHERE GEOGRAPHY :: STGeomFromWKB (
- @boundingBox, 4326) .STIntersects (CountryBoundary) = 1
- )
* This source code was highlighted with Source Code Highlighter .
- [Function (IsComposable = true )]
- public IQueryable <Boundaries_Country> f_bbx_Boundaries_Country (
- [Parameter (DbType = "varbinary (max)" )] byte [] boundingBox)
- {
- return this .CreateMethodCallQuery <Boundaries_Country> ( this ,
- ((MethodInfo) (MethodInfo.GetCurrentMethod ())), boundingBox);
- }
* This source code was highlighted with Source Code Highlighter .
- var q = from item in db.f_bbx_Boundaries_Country (
- sqlEnvelope.STAsBinary (). Buffer)
- where item.CountryName.StartsWith ( "C" )
- select item;
- foreach ( var item in q)
- Console .WriteLine (item.CountryName);

Source: https://habr.com/ru/post/49358/
All Articles