alex_bn_lee

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

【023】◀▶ C#学习(十) - 泛型&LINQ

LINQ(语言集成查询)

--------------------------------------------------------------------------------------------------------------

            ╔════════╗
╠════╣    第A1个    ╠══════════════════════════════════════════════════╣
            ╚════════╝

●·● LINQ(语言集成查询)

1. 语言集成查询 (LINQ) 是 Visual Studio 2008 中引入的一组功能,可为 C# 和 Visual Basic 语言语法提供强大的查询功能。 LINQ 引入了标准、易学的数据查询和更新模式,该技术可以扩展为几乎支持任何类型的数据存储。Visual Studio 包含 LINQ 提供程序的程序集,借助这些程序集,就能将 LINQ 用于 .NET Framework 集合、SQL Server 数据库、ADO.NET 数据集和 XML 文档。

2. 语言集成查询 (LINQ) 是 Visual Studio 2008 和 .NET Framework 3.5 版中引入的一项创新功能,它在对象领域和数据领域之间架起了一座桥梁。

  举例:泛型

复制代码
        private void button1_Click(object sender, EventArgs e)
{
int[] scores = new int[] { 88, 89, 100, 51, 23, 92, 81, 60 };
IEnumerable<int> scoreQuery =
from score in scores
where score > 70
select score;
foreach (int i in scoreQuery)
{
richTextBox1.Text += i + " ";
}
}
复制代码

  举例:var 关键字

 

复制代码
        private void button2_Click(object sender, EventArgs e)
{
int[] numbers = new int[] { 0, 1, 2, 3, 4, 5, 6, 7 };
var numQuery =
from num in numbers
where (num % 2) == 0
select num;
foreach (int num in numQuery)
{
richTextBox1.Text += "\t" + num.ToString() + "\n";
}
}
复制代码

 

--------------------------------------------------------------------------------------------------------------

            ╔════════╗
╠════╣    第A1个    ╠══════════════════════════════════════════════════╣
            ╚════════╝

●·● LINQ 查询表达式

1. 语言集成查询 (LINQ) 是一组技术的名称,这些技术建立在将查询功能直接集成到 C# 语言(以及 Visual Basic 和可能的任何其他 .NET 语言)的基础上。 借助于 LINQ,查询现在已是高级语言构造,就如同类、方法、事件等等。

2. 查询表达式必须以 from 子句开头,并且必须以 selectgroup 子句结尾。 在第一个 from 子句和最后一个 selectgroup 子句之间,查询表达式可以包含一个或多个下列可选子句:whereorderbyjoinlet 甚至附加的 from 子句。 还可以使用 into 关键字使 joingroup 子句的结果能够充当同一查询表达式中附加查询子句的源。

 

int highScoreCount =
(from score in scores
where score > 80
select score)
.Count();


--------------------------------------------------------------------------------------------------------------

            ╔════════╗
╠════╣    第A1个    ╠══════════════════════════════════════════════════╣
            ╚════════╝

●·● LINQ to SQL

1. LINQ to SQL 是 .NET Framework 3.5 版的一个组件,提供了用于将关系数据作为对象管理的运行时基础结构。

2. 在 Visual Studio 2010 上的建立过程:项目》右键》添加》新建项》LINQ to SQL

 

3. 将数据库的表直接拉到 DataClass2.dbml 中即可以建立连接!

 

导入数据:

复制代码
            OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() != DialogResult.OK) //选择要导入数据的文件
return;
DataClasses1DataContext ctx = new DataClasses1DataContext();
using (StreamReader reader = new StreamReader(File.OpenRead(ofd.FileName), System.Text.Encoding.Default))
{
string str;
while ((str = reader.ReadLine()) != null)
{
IdentityCard Card = new IdentityCard(); //新建一个 Card 实例,加入数据
string[] strs = str.Split('\t');
string no = strs[1];
string address = strs[2];
Card.No = no;
Card.Address = address;
ctx.IdentityCard.InsertOnSubmit(Card); //提交数据
ctx.SubmitChanges();
}
}
复制代码





 

posted on   McDelfino  阅读(356)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示