不错呦!smile@林凯西,确保“准备文件”中的几个文件都有安装,S...您好,看了您这篇帖子觉得很有帮助。但是有个问题想请...我的修改过了怎么还被恶意注册呢 @jjjjiiii 用PJ快9年了,主要是A...PJ3啊,貌似很少有人用PJ了,现在不是WP就是z...@332347365,我当时接入时错误码没有-10...楼主,ChkValue值应为-103是什么意思呢?...大哥 你最近能看到我发的信息,请跟我联系,我有个制...
语言集成查询(LINQ) 语法简介
编辑:dnawo 日期:2011-08-09
为什么要有LINQ呢?我们引用MSDN中的一段话:
引用内容
可见,LINQ主要是针对不同数据源查询提供了统一的解决方案,简化了学习、使用过程。
一、LINQ查询语法
二、LINQ查询简单示例
例1:int数组排序
例2:使用 LINQ 查询 ArrayList
例3:筛选对象
例4.用匿名类型返回多字段
三、补充说明
[1].LINQ数据源类型必须为IEnumerable、IEnumerable<T> 及其派生类型(如 IQueryable<T>);当数据源是非泛型IEnumerable 类型时,必须显式指定范围变量类型(例2)。
[2].创建查询时没有实际的去查询数据源,查询变量本身只是存储查询命令,实际的查询执行会延迟到在 foreach 语句中循环访问查询变量时发生。
四、参考资料
@.C# 中的 LINQ 入门:http://msdn.microsoft.com/zh-cn/library/bb397933(v=VS.90).aspx
@.查询关键字:http://msdn.microsoft.com/zh-cn/library/bb310804(v=VS.90).aspx

查询是一种从数据源检索数据的表达式。查询通常用专门的查询语言来表示。随着时间的推移,人们已经为各种数据源开发了不同的语言;例如,用于关系数据库的 SQL 和用于 XML 的 XQuery。因此,开发人员不得不针对他们必须支持的每种数据源或数据格式而学习新的查询语言。LINQ 通过提供一种跨各种数据源和数据格式使用数据的一致模型,简化了这一情况。在 LINQ 查询中,始终会用到对象。可以使用相同的基本编码模式来查询和转换 XML 文档、SQL 数据库、ADO.NET 数据集、.NET 集合中的数据以及对其有 LINQ 提供程序可用的任何其他格式的数据。
可见,LINQ主要是针对不同数据源查询提供了统一的解决方案,简化了学习、使用过程。
一、LINQ查询语法
复制内容到剪贴板
程序代码

//1.获取数据源[1]
//TODO
//2.创建查询[2]
var 查询变量 = form 范围变量 in 数据源
select 范围变量;
//3.执行查询
foreach(var 变量 in 查询变量);
//TODO
//2.创建查询[2]
var 查询变量 = form 范围变量 in 数据源
select 范围变量;
//3.执行查询
foreach(var 变量 in 查询变量);
二、LINQ查询简单示例
例1:int数组排序
复制内容到剪贴板
程序代码

using System;
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[] ids = new int[5] { 1, 2, 3, 4, 5 };
var query = from id in ids
orderby id descending
select id;
foreach (var id in query)
Console.WriteLine(id);
Console.ReadKey();
}
}
}
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[] ids = new int[5] { 1, 2, 3, 4, 5 };
var query = from id in ids
orderby id descending
select id;
foreach (var id in query)
Console.WriteLine(id);
Console.ReadKey();
}
}
}
例2:使用 LINQ 查询 ArrayList
复制内容到剪贴板
程序代码

using System;
using System.Collections;
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ArrayList ids = new ArrayList(5) { 1, 2, 3, 4, 5 };
var query = from int id in ids
orderby id descending
select id;
foreach (var id in query)
Console.WriteLine(id);
Console.ReadKey();
}
}
}
using System.Collections;
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ArrayList ids = new ArrayList(5) { 1, 2, 3, 4, 5 };
var query = from int id in ids
orderby id descending
select id;
foreach (var id in query)
Console.WriteLine(id);
Console.ReadKey();
}
}
}
例3:筛选对象
复制内容到剪贴板
程序代码

using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication1
{
class Student
{
public string Name { get; set; }
public int Score { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<Student> students = new List<Student>
{
new Student{Name="Zhang San",Score = 65},
new Student{Name="Li Si",Score = 80},
new Student{Name="Wang Wu",Score = 98}
};
var query = from s in students
where s.Score > 80
select s.Name;
foreach (var s in query)
Console.WriteLine(s);
Console.ReadKey();
}
}
}
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication1
{
class Student
{
public string Name { get; set; }
public int Score { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<Student> students = new List<Student>
{
new Student{Name="Zhang San",Score = 65},
new Student{Name="Li Si",Score = 80},
new Student{Name="Wang Wu",Score = 98}
};
var query = from s in students
where s.Score > 80
select s.Name;
foreach (var s in query)
Console.WriteLine(s);
Console.ReadKey();
}
}
}
例4.用匿名类型返回多字段
复制内容到剪贴板
程序代码

using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication1
{
class Student
{
public string Name { get; set; }
public int Score { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<Student> students = new List<Student>
{
new Student{Name="Zhang San",Score = 65},
new Student{Name="Li Si",Score = 80},
new Student{Name="Wang Wu",Score = 98}
};
var query = from s in students
where s.Score > 80
select new { Name = s.Name, Score = s.Score };//匿名类型
foreach (var s in query)
Console.WriteLine(s.Name + " " + s.Score);
Console.ReadKey();
}
}
}
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication1
{
class Student
{
public string Name { get; set; }
public int Score { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<Student> students = new List<Student>
{
new Student{Name="Zhang San",Score = 65},
new Student{Name="Li Si",Score = 80},
new Student{Name="Wang Wu",Score = 98}
};
var query = from s in students
where s.Score > 80
select new { Name = s.Name, Score = s.Score };//匿名类型
foreach (var s in query)
Console.WriteLine(s.Name + " " + s.Score);
Console.ReadKey();
}
}
}
三、补充说明
[1].LINQ数据源类型必须为IEnumerable、IEnumerable<T> 及其派生类型(如 IQueryable<T>);当数据源是非泛型IEnumerable 类型时,必须显式指定范围变量类型(例2)。
[2].创建查询时没有实际的去查询数据源,查询变量本身只是存储查询命令,实际的查询执行会延迟到在 foreach 语句中循环访问查询变量时发生。
四、参考资料
@.C# 中的 LINQ 入门:http://msdn.microsoft.com/zh-cn/library/bb397933(v=VS.90).aspx
@.查询关键字:http://msdn.microsoft.com/zh-cn/library/bb310804(v=VS.90).aspx
评论: 0 | 引用: 0 | 查看次数: 4210
发表评论
请登录后再发表评论!