这个程序中的LINQ 查询语句使用了LINQ 声明性查询语法:
本例中就是声明并初始化names 数组:
string[] names = { “Alonso”, “Zheng”, “Smith”, “Jones”, “Smythe”, “Small”,
“Ruiz”, “Hsieh”, “Jorgenson”, “Ilyich”, “Singh”, “Samba”, “Fatimah” };
var queryResults =
from n in names
where n.StartsWith(“S”)
select n;
该语句包括4 个部分:以var 开头的结果变量声明,使用查询表达式给该结果变量赋值,查询
表达式包含from 子句、where 子句和select 子句
var 是C#中的一个新关键字,用于声明一般的变量类型
queryResult 名称是随意指定的,可以把结果命名为任何名称
from n in names 本例中的数据源是前面声明的字符串数组names。变量n 只是数据源中某一元素的代表,类似于foreach 语句后面的变量名。指定from 子句,就可以只查找集合的一个子集,而不用迭代所有的元素。
说到迭代,LINQ 数据源必须是可枚举的——即必须是数组或集合,以便从中选择出一个或多个元素。
where n.StartsWith(“S”) 可以在where 子句中指定能应用于数据源中各元素的任意布尔(true 或false)表达式。实际上,where 子句是可选的,甚至可以忽略,但在大多数情况下,都要指定where 条件,把结果限制为我们需要的数据。where 子句称为LINQ 中的限制运算符, 因为它限制了查询的结果。
这个示例指定name 字符串以字母S 开头,还可以给字符串指定其他条件,例如,长度超过
10(where n.Length > 10)或者包含Q【where n.Contains(“Q”)】。
select n; select 子句是必须的,因为必须指定结果集中有哪些元素。这个结果集并不是很有趣,因为在结果集的每个元素中都只有一项name。如果结果集中有比较复杂的对象,使用select 子句的有效性就比较明显
LINQ查询的基本表达式
数据类型 集合名= from 变量名 in 集合 //
where 条件
orderby 字段,排序的关键字
select 变量名;
var newStudents = from s in students//数据类型不确定时用:var,不要去确定它的类型,返回结果不定。from s in students 变量S只是数据源中某一元素代表。类似于foreach
where s.Age>=22//查询条件,没有查询条件时获取到所有数据。
orderby s.id descending//降序排序。可省略。
select s//反回对象,可以是字符串、整数等,例:S.name或s.age。
//select s.name //只得到学生姓名。不需要返回所有字段。
//使用方法语法查询。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string strID = "";
//数据库连接字符串
string strCon = "Data Source=.;Initial Catalog=NiuDB20191013;Integrated Security=True";
//查询数据库
private void button1_Click(object sender, EventArgs e)
{
linQtoDataClasses1DataContext connLinq = new linQtoDataClasses1DataContext(strCon);
var result = from info in connLinq.NiuTable_202006156
select new
{
ID = info.ID,
姓名 = info.Name,
编号 = info.Code
};
dataGridView1.DataSource = result;
}
//向数据库中填加一行数据
private void button2_Click(object sender, EventArgs e)
{
//实例化Linq连接对象
linQtoDataClasses1DataContext linq = new linQtoDataClasses1DataContext(strCon);
//实例化tb_Employee类对象
NiuTable_202006156 employee = new NiuTable_202006156();
//为tb_Employee类中的员工实体赋值
employee.ID = Convert.ToInt32(textBoxID.Text);
employee.Name = textBoxName.Text;
employee.Code = Convert.ToInt32(textBoxCode.Text);
linq.NiuTable_202006156.InsertOnSubmit(employee); //添加员工信息
linq.SubmitChanges(); //提交操作
MessageBox.Show("数据添加成功");
}
//获取选种数据
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//实例化Linq连接对象
linQtoDataClasses1DataContext linq = new linQtoDataClasses1DataContext(strCon);
//获取选中的员工编号
textBoxID.Text = Convert.ToString(dataGridView1[0, e.RowIndex].Value).Trim();
//根据选中的员工编号获取其详细信息,并重新成成一个表
var result = from info in linq.NiuTable_202006156
where info.ID == Convert.ToInt32(textBoxID.Text)
select new
{
id = info.ID,
name = info.Name,
code = info.Code
};
//相应的文本框及下拉列表中显示选中员工的详细信息
foreach (var item in result)
{
textBoxID.Text = item.id.ToString();
textBoxName.Text = item.name;
textBoxCode.Text = item.code.ToString();
}
}
//向数据库中写入修改数据
private void button3_Click(object sender, EventArgs e)
{
linQtoDataClasses1DataContext linq = new linQtoDataClasses1DataContext(strCon); //实例化Linq连接对象
//查找要修改的员工信息
var result = from employee in linq.NiuTable_202006156
where employee.ID == Convert.ToInt32(textBoxID.Text)
select employee;
//对指定的员工信息进行修改
foreach (NiuTable_202006156 YG in result)//YG ==员工
{
YG.ID = Convert.ToInt32(textBoxID.Text);
YG.Name = textBoxName.Text;
YG.Code = Convert.ToInt32(textBoxCode.Text);
linq.SubmitChanges();
}
MessageBox.Show("员工信息修改成功");
}
//表格中任意位置获取行号
private void dgvInfo_CellClick(object sender, DataGridViewCellEventArgs e)
{
//获取选中的员工编号
strID = Convert.ToString(dataGridView1[0, e.RowIndex].Value).Trim();
}
//删除选种行的数据
private void button4_Click(object sender, EventArgs e)
{
linQtoDataClasses1DataContext linq = new linQtoDataClasses1DataContext(strCon);//实例化Linq连接对象
//查找要删除的员工信息
var result = from employee in linq.NiuTable_202006156
where employee.ID == Convert.ToInt32(strID)
select employee;
linq.NiuTable_202006156.DeleteAllOnSubmit(result); //删除员工信息
linq.SubmitChanges(); //实例化Linq连接对象提交操作
MessageBox.Show("员工信息删除成功");
}
}
}