Basic Query Operations

本文介绍了LINQ的基础查询语法,包括筛选、排序、联接及分组等操作,并提供了丰富的示例代码,帮助读者掌握LINQ的基本用法。

LINQ 的查询语法和 SQL 非常相似,只不过为了智能提示和类型推断将 select 子句放在最后。

int[] nums = { 1, 2, 3, 4 };
var q = from n in nums select n;


"from n in nums" 中的两个变量用法类似 "foreach (var n in nums)","select n" 用于返回当前变量。

1. Filtering

所谓 Filtering,就是 where 子句,用于进行条件过滤。

Query Syntax

int[] nums = { 1, 2, 3, 4 };
var q = from n in nums where n % 2 == 0 select n;

foreach (var v in q)
  Console.WriteLine(v);


Method Syntax

var q = nums.Where(n => n % 2 == 0);


输出:
2
4

2. Ordering

使用 "orderby ... ascending" 或 "orderby ... descending" 子句可以实现对结果的排序操作。

Query Syntax

int[] nums = { 1, 2, 3, 4 };
var q = from n in nums orderby n descending select n;

foreach (var v in q)
  Console.WriteLine(v);


Method Syntax

var q = nums.OrderByDescending(n => n);


输出:
4
3
2
1

3. Joining

下面这个例子稍微有点复杂,我们的意图是将 User.Sex 从整数转换成对应的性别字符。 "join...in..." 的作用是将额外的数据源合并进来,而 "on...equals..." 则用于说明合并的条件,最后我们使用匿名类型来创建所需的新数据容器。

Query Syntax

var sex = new[] {
       new { Id = 0, Name = "女"},
       new { Id = 1, Name = "男"}
     };

var users = new[] {
 new { Name = "User1", Sex = 1 },
 new { Name = "User2", Sex = 0 },
 new { Name = "User3", Sex = 1 },
 new { Name = "User4", Sex = 0 },
 new { Name = "User5", Sex = 1 },
 new { Name = "User6", Sex = 0 },
 new { Name = "User7", Sex = 1 },
 new { Name = "User8", Sex = 0 },
 new { Name = "User9", Sex = 1 }
 };

var q = from u in users
    join s in sex
    on u.Sex equals s.Id
    select new { Name = u.Name, Sex = s.Name };

foreach (var u in q)
  Console.WriteLine("Name:{0} Sex:{1}", u.Name, u.Sex);


Method Syntax

var q = users.Join(sex, u => u.Sex, s => s.Id, (u, s) => new { Name = u.Name, Sex = s.Name });


输出:
Name:User1 Sex:男
Name:User2 Sex:女
Name:User3 Sex:男
Name:User4 Sex:女
Name:User5 Sex:男
Name:User6 Sex:女
Name:User7 Sex:男
Name:User8 Sex:女
Name:User9 Sex:男

4. Grouping

使用 "group...by..." 可以按照一定的条件进行分组返回,该语句可以省略 select 子句。

Query Syntax

var users = new[] {
 new { Name = "User1", Sex = 1 },
 new { Name = "User2", Sex = 0 },
 new { Name = "User3", Sex = 1 },
 new { Name = "User4", Sex = 0 },
 new { Name = "User5", Sex = 1 },
 new { Name = "User6", Sex = 0 },
 new { Name = "User7", Sex = 1 },
 new { Name = "User8", Sex = 0 },
 new { Name = "User9", Sex = 1 }
};

var q = from u in users group u by u.Sex;

foreach (var group in q)
{
  Console.WriteLine("---------------");

  foreach (var u in group)
    Console.WriteLine(u.Name);
}


Method Syntax

var q = users.GroupBy(u => u.Sex);


输出:
---------------
User1
User3
User5
User7
User9
---------------
User2
User4
User6
User8

### PostGIS 3.5.2 Installation and Usage Guide PostGIS is an extension to the PostgreSQL object-relational database that allows geographic objects to be stored in the database. Below are detailed instructions on how to install PostGIS version 3.5.2 along with its dependencies. #### Prerequisites Before installing PostGIS, ensure you have installed all necessary prerequisites such as GEOS, PROJ, GDAL, JSON-C, LibXML2, Protobuf, and SFCGAL libraries. These can typically be compiled from source or obtained via package managers depending on your operating system environment[^4]. #### Step-by-step Compilation of Dependencies 1. **Install GEOS** ```bash tar jxf geos-3.5.2.tar.bz2 cd geos-3.5.2/ ./configure --prefix=/usr/local/geos make && make install ``` 2. **Install PROJ (Version 4 Example)** ```bash tar jxf proj-4.9.2.tar.gz cd proj-4.9.2/ ./configure --prefix=/usr/local/proj4 make && sudo make install ``` 3. **Install GDAL** Download the latest stable release: ```bash wget https://download.osgeo.org/gdal/3.5.2/gdal-3.5.2.tar.gz tar -zxvf gdal-3.5.2.tar.gz cd gdal-3.5.2/ ./configure --prefix=/data/pgsql/postgis/gdal --with-proj=/data/pgsql/postgis/proj make && make install ``` 4. **Install JSON-C Library** Ensure `json-c` library is available at a specified path like `/data/pgsql/postgis/jsonc`. 5. **Configure Dynamic Linker Paths** To allow PostgreSQL to locate these third-party libraries during runtime, configure dynamic linker paths by editing the configuration file. ```bash echo "/data/pgsql/pg14/lib" >> /etc/ld.so.conf.d/postgis-aarch64.conf echo "/data/pgsql/postgis/proj6/lib" >> /etc/ld.so.conf.d/postgis-aarch64.conf echo "/data/pgsql/postgis/gdal/lib" >> /etc/ld.so.conf.d/postgis-aarch64.conf ldconfig ``` This ensures proper linking between PostgreSQL and spatial extensions[^5]. #### Installing PostGIS Extension Once all required components are ready: 1. Extract the PostGIS archive: ```bash tar zxf postgis-3.5.2.tar.gz cd postgis-3.5.2/ ``` 2. Configure build options specifying custom prefixes where applicable: ```bash ./configure \ --with-pgconfig=/path/to/your/pg_config \ --with-geosconfig=/usr/local/geos/bin/geos-config \ --with-projdir=/data/pgsql/postgis/proj6 \ --with-gdalconfig=/data/pgsql/postgis/gdal/bin/gdal-config \ --with-jsondir=/data/pgsql/postgis/jsonc/include ``` 3. Compile and Install: ```bash make && make install ``` After successful compilation, proceed to create templates within PostgreSQL databases enabling support for GIS operations using SQL commands provided below. #### Basic Query Operations Using WKT Format Output Geometry Column Data Type (`ST_AsText`) To retrieve geometries represented as Well-Known Text strings after setting up tables containing geometry columns: ```sql SELECT name, ST_AsText(geom) FROM test2; ``` This command fetches both textual labels alongside their corresponding geometric representations encoded into readable formats suitable for further processing outside relational storage systems[^3]. --- ### §§Related Questions§§ 1. How do I troubleshoot issues when configuring PostGIS against specific versions of dependent libraries? 2. What steps should one follow while upgrading existing installations involving multiple interdependent packages including PostgreSQL itself? 3. Can we automate parts of this manual process through scripting tools without losing flexibility over customization choices made earlier stages? 4. Are there any notable differences between compiling older versus newer releases concerning compatibility across platforms? 5. Which additional features become accessible once integrating advanced computational geometry algorithms offered under optional modules bundled inside larger distributions?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值