Entity Framework 学习初级篇5--ObjectQuery查询及方法
ObjectQuery 类支持对 实体数据模型 (EDM) 执行 LINQ to Entities 和 Entity SQL 查询。ObjectQuery 还实现了一组查询生成器方法,这些方法可用于按顺序构造等效于 Entity SQL 的查询命令。下面是 ObjectQuery 的查询生成器方法以及等效的 Entity SQL 语句:
Distinct,Except,GroupBy,Intersect,OfType,OrderBy,Select,SelectValue,Skip,Top,Union,UnionAll,Where
每个查询生成器方法返回 ObjectQuery 的一个新实例。使用这些方法可以构造查询,而查询的结果集基于前面ObjectQuery 实例序列的操作。下面来看具体的代码片断:
l Execute方法:
using (var edm = new NorthwindEntities())
{
string esql = "select value c from NorthwindEntities.Customers as c order by c.CustomerID limit 10";
ObjectQuery<Customers> query = edm.CreateQuery<Customers>(esql);
ObjectResult<Customers> results = query.Execute(MergeOption.NoTracking);
Assert.AreEqual(results.Count(), 10);
foreach (Customers c in query)
Console.WriteLine(c.CustomerID);
}
其中需要说明的是: MergeOption这个枚举类型的参数项,MergeOption有四种值分别是:
l AppendOnly: 只追加新实体,不修改以前获取的现有实体。这是默认行为。
l OverwriteChanges: 将 ObjectStateEntry 中的当前值替换为存储区中的值。这将使用服务器上的数据重写在本地所做的更改。
l PreserveChanges: 将替换原始值,而不修改当前值。这对于在发生开放式并发异常之后强制成功保存本地值非常有用。
l NoTracking: 将不修改 ObjectStateManager,不会获取与其他对象相关联的关系,可以改善性能。
l GetResultType方法:返回查询结果的类型信息.例如:
using (var edm = new NorthwindEntities())
{
string esql = "select value c from NorthwindEntities.Customers as c order by c.CustomerID limit 10";
ObjectQuery<Customers> query = edm.CreateQuery<Customers>(esql);
Console.WriteLine(query.GetResultType().ToString());
//输出结果为:
//NorthWindModel.Customers
}
l ToTraceString方法:获取当前执行的SQL语句。
l Where
实例代码如下:
using (var edm = new NorthwindEntities())
{
string esql = "select value c from NorthwindEntities.Customers as c ";
ObjectQuery<Customers> query1 = edm.CreateQuery<Customers>(esql);
//使用ObjectParameter的写法
query1 = query1.Where("it.CustomerId=@customerid");
query1.Parameters.Add(new ObjectParameter("customerid", "ALFKI"));
//也可以这样写
//ObjectQuery<Customers> query2 = edm.Customers.Where("it.CustomerID='ALFKI'");
foreach (var c in query1)
Console.WriteLine(c.CustomerID);
//显示查询执行的SQL语句
Console.WriteLine(query1.ToTraceString());
}
l First/ FirstOrDefault
实例代码如下:
using (var edm = new NorthwindEntities())
{
string esql = "select value c from NorthwindEntities.Customers as c order by c.CustomerID limit 10";
ObjectQuery<Customers> query = edm.CreateQuery<Customers>(esql);
Customers c1 = query.First();
Customers c2 = query.FirstOrDefault();
Console.WriteLine(c1.CustomerID);
Assert.IsNotNull(c2);
Console.WriteLine(c2.CustomerID);
}
l Distinct
实例代码如下:
using (var edm = new NorthwindEntities())
{
string esql = "select value c.City from NorthwindEntities.Customers as c order by c.CustomerID limit 10";
ObjectQuery<string> query = edm.CreateQuery<string>(esql);
query = query.Distinct();
foreach (string c in query)
{
Console.WriteLine("City {0}", c);
}
}
l Except:返回两个查询的差集。实例代码如下:
using (var edm = new NorthwindEntities())
{
string esql1 = "select value c from NorthwindEntities.Customers as c order by c.CustomerID limit 10";
ObjectQuery<Customers> query1 = edm.CreateQuery<Customers>(esql1);
string esql2 = "select value c from NorthwindEntities.Customers as c where c.Country='UK' order by c.CustomerID limit 10";
ObjectQuery<Customers> query2 = edm.CreateQuery<Customers>(esql2);
query1 = query1.Except(query2);
foreach (Customers c in query1)
{
Console.WriteLine(c.Country);
//输出:UK
}
}
l Intersect:返回两个查询的交集。实例代码如下:
using (var edm = new NorthwindEntities())
{
string esql1 = "select value c from NorthwindEntities.Customers as c order by c.CustomerID limit 10";
ObjectQuery<Customers> query1 = edm.CreateQuery<Customers>(esql1);
string esql2 = "select value c from NorthwindEntities.Customers as c where c.Country='UK' order by c.CustomerID limit 10";
ObjectQuery<Customers> query2 = edm.CreateQuery<Customers>(esql2);
query1 = query1.Intersect(query2);
foreach (Customers c in query1)
{
Console.WriteLine(c.Country);
}
}
l Union/UnionAll:返回两个查询的合集,包括重复项。其中UnionAll必须是相同类型或者是可以相互转换的。
l Include:可通过此方法查询出与相关的实体对象。实例代码如下:
using (var edm = new NorthwindEntities())
{
string esql1 = "select value c from NorthwindEntities.Customers as c WHERE c.CustomerID ='HANAR'";
ObjectQuery<Customers> query1 = edm.CreateQuery<Customers>(esql1);
query1 = query1.Include("Orders");
foreach (Customers c in query1)
{
Console.WriteLine("{0},{1}", c.CustomerID, c.Orders.Count);
//输出:HANAR,14
}
}
l OfType: 根据制定类筛选元素创建一个新的类型。此类型是要在实体模型中已定义过的。
l OrderBy
实例代码如下:
using (var edm = new NorthwindEntities())
{
string esql1 = "select value c from NorthwindEntities.Customers as c order by c.CustomerID limit 10";
ObjectQuery<Customers> query1 = edm.CreateQuery<Customers>(esql1);
query1.OrderBy("it.country asc,it.city asc");
//也可以这样写
//query1.OrderBy("it.country asc");
//query1.OrderBy("it.city asc");
foreach (Customers c in query1)
{
Console.WriteLine("{0},{1}", c.Country, c.City);
}
}
l Select
实例代码如下:
using (var edm = new NorthwindEntities())
{
string esql1 = "select value c from NorthwindEntities.Customers as c order by c.CustomerID limit 10";
ObjectQuery<Customers> query1 = edm.CreateQuery<Customers>(esql1);
ObjectQuery<DbDataRecord> records = query1.Select("it.customerid,it.country");
foreach (DbDataRecord c in records)
{
Console.WriteLine("{0},{1}", c[0], c[1]);
}
Console.WriteLine(records.ToTraceString());
//SQL输出:
//SELECT TOP (10)
//1 AS [C1],
//[Extent1].[CustomerID] AS [CustomerID],
//[Extent1].[Country] AS [Country]
//FROM [dbo].[Customers] AS [Extent1]
//ORDER BY [Extent1].[CustomerID] ASC
}
l SelectValue
实例代码如下:
using (var edm = new NorthwindEntities())
{
string esql1 = "select value c from NorthwindEntities.Customers as c order by c.CustomerID limit 10";
ObjectQuery<Customers> query1 = edm.CreateQuery<Customers>(esql1);
ObjectQuery<string> records = query1.SelectValue<string>("it.customerid");
foreach (string c in records)
{
Console.WriteLine("{0}", c);
}
Console.WriteLine(records.ToTraceString());
//SQL输出:
//SELECT TOP (10)
//[Extent1].[CustomerID] AS [CustomerID]
//FROM [dbo].[Customers] AS [Extent1]
//ORDER BY [Extent1].[CustomerID] ASC
}
l Skip/Top
实例代码如下:
using (var edm = new NorthwindEntities())
{
string esql1 = "select value c from NorthwindEntities.Customers as c order by c.CustomerID ";
ObjectQuery<Customers> query1 = edm.CreateQuery<Customers>(esql1);
query1 = query1.Skip("it.customerid asc", "10");
query1 = query1.Top("10");
foreach (Customers c in query1)
{
Console.WriteLine("{0}", c.CustomerID);
}
Console.WriteLine(query1.ToTraceString());
//SQL输出:
//SELECT TOP (10)
//[Extent1].[CustomerID] AS [CustomerID]
//FROM [dbo].[Customers] AS [Extent1]
//ORDER BY [Extent1].[CustomerID] ASC
}
本节,简单的介绍一下与ObjectQuery查询相关的语法,我个人觉得查询写法比较多,需要在日常的编程中去发现,在这里就不一一复述了。下节,将介绍EntityClient相关的内容。
Entity Framework 学习初级篇5--ObjectQuery查询及方法的更多相关文章
- Entity Framework学习初级篇2
Entity Framework 学习初级篇2--ObjectContext.ObjectQuery.ObjectStateEntry.ObjectStateManager类的介绍 本节,简单的介绍E ...
- Entity Framework 学习初级篇--基本操作:增加、更新、删除、事务(转)
摘自:http://www.cnblogs.com/xray2005/archive/2009/05/17/1458568.html 本节,直接写通过代码来学习.这些基本操作都比较简单,与这些基本操作 ...
- Entity Framework 学习初级篇1--EF基本概况
转自:http://www.cnblogs.com/Tally/archive/2012/09/14/2685011.html 最近在学习研究微软的EF,通过这时间的学习研究,感觉这个EF目前来说还不 ...
- Entity Framework 学习初级篇7--基本操作:增加、更新、删除、事务
本节,直接写通过代码来学习.这些基本操作都比较简单,与这些基本操作相关的内容在之前的1至6节基本介绍完毕. l 增加: 方法1:使用AddToXXX(xxx)方法:实例代码如下: ...
- Entity Framework学习初级篇1--EF基本概况《转》
最近在学习研究微软的EF,通过这时间的学习研究,感觉这个EF目前来说还不是很完善,半成品.不过,据说在.Net4.0中,微软将推荐使用此框架,并会有所改善.而且,现在基本上所有数据库均提供了对EF的支 ...
- Entity Framework 学习初级篇2--ObjectContext、ObjectQuery、ObjectStateEntry、ObjectStateManager类的介绍
本节,简单的介绍EF中的ObjectContext.ObjectQuery.ObjectStateEntry.ObjectStateManager这个几个比较重要的类,它们都位于System.Data ...
- Entity Framework 学习初级篇2--ObjectContext类的介绍
转自:http://www.cnblogs.com/Tally/archive/2012/09/14/2685014.html 本节,简单的介绍EF中的ObjectContext.ObjectQuer ...
- Entity Framework 学习初级篇3-- LINQ TO Entities
LINQ 技术(即 LINQ to Entities)使开发人员能够通过使用 LINQ 表达式和 LINQ 标准查询运算符,直接从开发环境中针对 实体框架对象上下文创建灵活的强类型查询.LINQ to ...
- Entity Framework学习初级篇3--LINQ TO Entities
LINQ 技术(即LINQ to Entities)使开发人员能够通过使用LINQ 表达式和LINQ 标准查询运算符,直接从开发环境中针对实体框架对象上下文创建灵活的强类型查询.LINQ to Ent ...
随机推荐
- DHCP 协议的 8 种消息类型及功能服务器
1) 主机发送 DHCP Discover 广播包在网络上寻找 DHCP 服务器: 单播数据包, 地址. 2) DHCP 服务器向主机发送 DHCP Offer 单播数据包,包含 IP 地址.MAC ...
- 《JavaScript高级程序设计》读书笔记 ---数据类型
ECMAScript 中有5 种简单数据类型(也称为基本数据类型):Undefined.Null.Boolean.Number.String和Object——复杂数据类型,Object 本质上是由一组 ...
- androidstudio--gsonformat--超爽的数据解析方式
很久以前写json解析用原始的解析json的方法,后来为了加快开发进度,开始使用gson,fastjson等第三方jar包来进行json解析,为了保持apk足够小,不因为引入jar包导致apk文件过大 ...
- leetcode404-----简单的树的遍历
Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...
- VS中的快捷键快速格式化代码,使好看,整齐
在VC2005中,快捷键是Ctrl + K, Ctrl + F, 这是一个组合键,即先按Ctrl + K, 这时候编辑器会等待下一个按键动作,此时再按Ctrl + F, 即可以格式化代码了,当然,也可 ...
- 第四十五节,logging日志模块
用于便捷记录日志且线程安全的模块 单文件日志 basicConfig()模块函数 功能:创建日志文件和写日志模式[有参] 使用方法:模块名称.basicConfig(filename="日志 ...
- 第十节,While循环和for循环
While循环 While循环,是一个循环加判断的组合,满足判断条件返回 真(True)开始循环代码块,不满足判断条件返回 假()不循环 格式: While 条件: 代码块 注意:在While循环里如 ...
- C#连接Oracle的方法
C#连接Oracle的方法 方法1: System.Data.OracleClient oracleConnectionString : data source = orcl;user id= sco ...
- ubuntu 软件管理
软件安装管理 安装软件 sudo apt-get install **** 检索软件 sudo apt-cache search **** 更新源 sudo apt-get update **** ...
- JavaConcurrentHashMap
JavaConcurrentHashMap 大滕翼不自觉地往前探了探身子可还没等他听到唐寅说到 兆咒耶Т √靼脶摹 す宿坩曹 '字'心'上都有横您老瞧瞧这个'宣德 炔灌莞 蹇忄в 幼銮亿辽 仄黛境 ...