现在的位置: 首页 > 综合 > 正文

Entity Framework 4.0之2:简单的LINQ 数据操作(1)

2012年02月16日 ⁄ 综合 ⁄ 共 4187字 ⁄ 字号 评论关闭

  .NET Language Integrated Query(LINQ)一体化查询语言,是集成在 .NET Framework 3.5 编程语言中的一种新特性,已成为编程语言的一部分,使开发人员可以使用语法基本一致的语句对不同来源不同类型的数据进行查询与整合,它使得查询表达式可以得到很好的编译时语法检查。

一:字符串查询

string 查询

string strLinq ="Hello World!";

var result
= from q in strLinq select q;

var result1
= from q in strLinq where q =='o' select q;

// Linq 的扩展方法
var result2 = strLinq.Where(q => q =='o');
var result3
= Enumerable.Where(strLinq, q => q =='o');

二:数组查询

数组查询

string[] strs ={ "Suyama", "Fuller", "Callahan", "Michael", "Janet" };

var result
= from p in strs where p.Length >5 select p;

var result1
= strs.Where(p => p.Length>5);

var result2
= strs.Where(p =>p.StartsWith("C"));

  

三:XML 查询

XML 内容

<Employees>
<Employee gender="0">
<Name>Davolio</Name>
<Address>507 - 20th Ave. E. Apt. 2A</Address>
</Employee>
<Employee gender="0">
<Name>Andrew</Name>
<Address>4110 Old Redmond Rd.</Address>
</Employee>
<Employee gender="1">
<Name>Laura</Name>
<Address>Coventry House Miner Rd.</Address>
</Employee>
<Employee gender="1">
<Name>Anne</Name>
<Address>4726 - 11th Ave. N.E.</Address>
</Employee>
<Employee gender="0">
<Name>King</Name>
<Address>7 Houndstooth Rd.</Address>
</Employee>
</Employees>

  

XML 查询

 XElement root = XElement.Load("D:\\Employees.xml");

// 查询性别为男性(gender=1)的全部员工
var emps = from s in root.Elements("Employee")
where s.Attribute("gender").Value =="1"
select s;

// 查询性别为女性(gender=0)的员工的姓名和住址
var emps1 = from s in root.Elements("Employee")
where s.Attribute("gender").Value =="0"
select
new
{
name
= s.Element("Name").Value,
address
= s.Element("Address").Value
};

  

四:数据库表查询

其中用到的表的结构以及数据大致为下图所示:

  

  

  

  (1) 单表查询

LINQ单表查询

// <1> 查询所有客户
var customers1 = from s in dc.Customers select s;
List
<Customer> customerLst1 = customers1.ToList();


// <2> 查询国籍为 'Germany' 的客户
var customers2 = from s in dc.Customers where s.Country =="Germany" select s;
List
<Customer> customerLst2 = customers2.ToList();

List
<Customer> customerLst2_1 = (from s in dc.Customers where s.Country =="Germany" select s).ToList();

List
<Customer> customerLst2_2 = dc.Customers.Where(s => s.Country =="Germany").ToList();


// <3> 按公司名降序排列查询员工ID和公司
var customerLst3 = (from s in dc.Customers
orderby s.CompanyName descending
select
new
{
ID
= s.CustomerID,
Company
= s.CompanyName
}).ToList();


// <4> 查询公司名,并判断其长度是不是大于20
var customerLst4 = (from s in dc.Customers
select
new
{
CompanyName
= s.CompanyName,
IsThan20
= (s.CompanyName.Length >20) ?true : false
}).ToList();


// <5> 按顺序查询第10到20记录的客户
List<Customer> customerLst5 = (from s in dc.Customers select s).Skip(9).Take(11).ToList();

// Skip(9): 跳过前9个
// Take(11): 取前11条记录


// <6> 国籍为 Canada 和 USA 的客户
var customerLst6 = from s in dc.Customers wherenewstring[] { "Canada", "USA" }.Contains(s.Country) select s;


// <7> 地址中有 '9'
var customerLst7 = from s in dc.Customers where s.Address.Contains("9") select s;


// <8> 地址以 'A' 开头
var customerLst8 = from s in dc.Customers where s.Address.StartsWith("A") select s;

var customerLst8_1
= from s in dc.Customers where s.Address.IndexOf("A") ==0 select s;


// <9> 地址以 'A' 开头的客户数量
var customerLst9 = dc.Customers.Count(s => s.Address.IndexOf("A") ==0);


// <10> 查询最高、最低、平均、总共的付款
var customerLst10 = dc.Customers.Select(s => s.Payment).Max();
var customerLst10_1
= Enumerable.Select(dc.Customers, s => s.Payment).Max();

var customerLst10_2
= dc.Customers.Select(s => s.Payment).Min();
var customerLst10_3
= dc.Customers.Select(s => s.Payment).Average();
var customerLst10_4
= dc.Customers.Select(s => s.Payment).Sum();


// <11> 按国籍查询客户数量和最高付款
var customerLst11 = (from s in dc.Customers
group s by s.Country into p
select
new
{
Country
= p.Key,
Count
= p.Count(),
Payment
= p.Max(g => g.Payment)
}).ToList();

  

  (2) 多表查询

LINQ多表查询

// <1> 查询每个客户下订单的日期 (内连接)
var customerLst1 = from s in dc.Customers
join p
in dc.Orders on s.CustomerID equals p.CustomerID
orderby s.CustomerID ascending
select
new
{
ID
= s.CustomerID,
OrderDate
= p.OrderDate
};


// <2> 查询每个客户下订单的日期 (左外连接)
var customerLst2 = from s in dc.Customers
join p
in dc.Orders on s.CustomerID equals p.CustomerID
into sp from a
in sp.DefaultIfEmpty()
orderby s.CustomerID ascending
select
new
{
ID
= s.CustomerID,
OrderDate
= a.OrderDate
};


// <3> 查询每个客户下订单的日期,条件:付款大于200且订单日期在1997-12-08以后 (多表条件查询)
var customerLst3 = from s in dc.Customers
join p
in dc.Orders on s.CustomerID equals p.CustomerID
where s.Payment >200&& p.OrderDate > DateTime.Parse("1997-12-08")
orderby s.CustomerID ascending
select
new
{
ID
= s.CustomerID,
OrderDate
= p.OrderDate
};

  

实际操作起来,用linq进行多表连接比较复杂(特别是在表很多的情况下),建议先将查询内容做成视图,再把视图映射成实体类,这样就可以用单表查询的方式进行查询了。

作者建议:LINQ在做对数据库查询的时候,其实就是对sql的再封装,从而使得操作更加简洁,而且从LINQ解析得到的sql也得到了优化,可能针对某些查询,比没有优化的sql查询效率更高些,所以,如果开发人员追求效率(对数据的增、删、改、查),建议先做存储过程,然后优化,再把这些优化的存储过程在DataContext里面封装成方法,再调用这些方法,这样既可以把sql写的更好,效率又更高,而且还对提高个人的数据库知识水平也有很大帮助。

抱歉!评论已关闭.