1. ObjectQuery<T> 提供了一个管理[实体对像]集合
2. ObjectQuery<T>继承System.Data.Objects.ObjectQuery, ObjectQuery对ObjectContext进行了封装,
3.可以通过ObjectContext.CreateQuery<T>("esql")的方式创建ObjectQuery<T>
4.可以通过new ObjectQuery<T>(ObjectContext,"esql")的方式创建ObjectQuery<T>,跟据 SQL字串的不同,会得到具体的ObjectQuery<值类型>,或ObjectQuery<DbDataRecord>或 ObjectQuery<实体>
string econString = @" metadata=res://*/myModel.csdl |res://*/myModel.ssdl |res://*/myModel.msl ; provider=System.Data.SqlClient; provider connection string="" Data Source=.; Initial Catalog=LingTestDB; Integrated Security=True; MultipleActiveResultSets=True; "" "; EntityConnection econ = new EntityConnection(econString); ObjectContext context = new ObjectContext(econ); context.DefaultContainerName = "myContext"; ObjectQuery<DBItem> queryTab = context.CreateQuery<DBItem>("DBItem"); foreach (var r in queryTab) { System.Console.WriteLine("{0},{1}", r.ItemID, r.ItemMatter); } |
string econString = @" metadata=res://*/myModel.csdl |res://*/myModel.ssdl |res://*/myModel.msl ; provider=System.Data.SqlClient; provider connection string="" Data Source=.; Initial Catalog=LingTestDB; Integrated Security=True; MultipleActiveResultSets=True; "" "; EntityConnection econ = new EntityConnection(econString); ObjectContext context = new ObjectContext(econ); ObjectQuery<DBItem> queryTab = context.CreateQuery<DBItem>("select value it from myContext.DBItem as it where it.ItemID='a'"); foreach (var r in queryTab) { System.Console.WriteLine("{0},{1}",r.ItemID,r.ItemMatter); } |
string econString = @" metadata=res://*/myModel.csdl |res://*/myModel.ssdl |res://*/myModel.msl ; provider=System.Data.SqlClient; provider connection string="" Data Source=.; Initial Catalog=LingTestDB; Integrated Security=True; MultipleActiveResultSets=True; "" "; EntityConnection econ = new EntityConnection(econString); ObjectContext context = new ObjectContext(econ); ObjectQuery<DbDataRecord> queryTab = new ObjectQuery<DbDataRecord>("select it.ItemID,it.ItemMatter from myContext.DBItem as it", context); foreach (var r in queryTab) { System.Console.WriteLine("{0},{1}",r[0].ToString(),r[1].ToString()); } |
string econString = @" metadata=res://*/myModel.csdl |res://*/myModel.ssdl |res://*/myModel.msl ; provider=System.Data.SqlClient; provider connection string="" Data Source=.; Initial Catalog=LingTestDB; Integrated Security=True; MultipleActiveResultSets=True; "" "; EntityConnection econ = new EntityConnection(econString); ObjectContext context = new ObjectContext(econ); ObjectQuery<int> queryTab = new ObjectQuery<int>("select value Count(it.ItemID) from myContext.DBItem as it", context); foreach (var r in queryTab) { System.Console.WriteLine("个数:{0}", r.ToString() ); } |
string econString = @" metadata=res://*/myModel.csdl |res://*/myModel.ssdl |res://*/myModel.msl ; provider=System.Data.SqlClient; provider connection string="" Data Source=.; Initial Catalog=LingTestDB; Integrated Security=True; MultipleActiveResultSets=True; "" "; EntityConnection econ = new EntityConnection(econString); ObjectContext context = new ObjectContext(econ); context.DefaultContainerName = "myContext"; ObjectQuery<DBItem> queryTab = context.CreateQuery<DBItem>("DBItem"); ObjectResult<DBItem> resultTab = queryTab.Execute(MergeOption.NoTracking); foreach (var r in resultTab) { System.Console.WriteLine("{0},{1}",r.ItemID,r.ItemMatter); } |
ObjectQuery<T>.Execute()方法返回ObjectResult<T>对象
ObjectQuery<TResultType> OfType<TResultType>(); |
myContext context = new myContext(); ObjectQuery<DBItemEx> v = context.DBItem.OfType<DBItemEx>(); |
判断集合中是否所有元素都满足某一条件 |
myContext context = new myContext(); bool b= context.DBItemList.All(p => p.ItemValue >= 0); |
SELECT CASE WHEN ( NOT EXISTS (SELECT cast(1 as bit) AS [C1] FROM [dbo].[DBItemList] AS [Extent1] WHERE ( NOT ([Extent1].[ItemValue] >= 1)) OR (CASE WHEN ([Extent1].[ItemValue] >= 0) THEN cast(1 as bit) WHEN ( NOT ([Extent1].[ItemValue] >= 0)) THEN cast(0 as bit) END IS NULL) )) THEN cast(1 as bit) WHEN ( EXISTS (SELECT cast(1 as bit) AS [C1] FROM [dbo].[DBItemList] AS [Extent2] WHERE ( NOT ([Extent2].[ItemValue] >= 0)) OR (CASE WHEN ([Extent2].[ItemValue] >= 0) THEN cast(1 as bit) WHEN ( NOT ([Extent2].[ItemValue] >= 0)) THEN cast(0 as bit) END IS NULL) )) THEN cast(0 as bit) END AS [C1] FROM ( SELECT cast(1 as bit) AS X ) AS [SingleRowTable1] |
判断集合中是否有元素满足某一条件 |
myContext context = new myContext(); bool b = context.DBItemList.Any(p => p.ItemValue == 4); |
SELECT CASE WHEN ( EXISTS (SELECT cast(1 as bit) AS [C1] FROM [dbo].[DBItemList] AS [Extent1] WHERE 4 = [Extent1].[ItemValue] )) THEN cast(1 as bit) WHEN ( NOT EXISTS (SELECT cast(1 as bit) AS [C1] FROM [dbo].[DBItemList] AS [Extent2] WHERE 4 = [Extent2].[ItemValue] )) THEN cast(0 as bit) END AS [C1] FROM ( SELECT cast(1 as bit) AS X ) AS [SingleRowTable1] |
获取集合的前n个元素 |
myContext context = new myContext(); IQueryable<DBItemList> list = context.DBItemList.Take(3); |
SELECT TOP (3) [c].[AutoId] AS [AutoId], [c].[NameID] AS [NameID], [c].[ItemID] AS [ItemID], [c].[ItemValue] AS [ItemValue] FROM [dbo].[DBItemList] AS [c] |
跳过集合的前n个元素, Linq To Entity 要求必须先OrderBy |
myContext context = new myContext(); IQueryable<DBItemList> list = context.DBItemList.OrderBy(p=>p.ItemValue).Skip(5); |
SELECT [Extent1].[AutoId] AS [AutoId], [Extent1].[NameID] AS [NameID], [Extent1].[ItemID] AS [ItemID], [Extent1].[ItemValue] AS [ItemValue] FROM ( SELECT [Extent1].[AutoId] AS [AutoId], [Extent1].[NameID] AS [NameID], [Extent1].[ItemID] AS [ItemID], [Extent1].[ItemValue] AS [ItemValue], row_number() OVER (ORDER BY [Extent1].[ItemValue] ASC) AS [row_number] FROM [dbo].[DBItemList] AS [Extent1] ) AS [Extent1] WHERE [Extent1].[row_number] > 5 ORDER BY [Extent1].[ItemValue] ASC |
集合的第一个元素,集合中没有会报错, |
myContext context = new myContext(); DBItemList f1 = context.DBItemList.First(); DBItemList fi = context.DBItemList.First(p => p.ItemValue == 5); |
SELECT TOP (1) [Extent1].[AutoId] AS [AutoId], [Extent1].[NameID] AS [NameID], [Extent1].[ItemID] AS [ItemID], [Extent1].[ItemValue] AS [ItemValue] FROM [dbo].[DBItemList] AS [Extent1] WHERE 5 = [Extent1].[ItemValue] |
集合中的第一个元素,没有则返回类型默认值,对象型默认值为null |
myContext context = new myContext(); DBItemList fi = context.DBItemList.FirstOrDefault(p => p.ItemValue ==5); if (fi != null) { Console.WriteLine(fi.ItemValue); } |
用LinqExpressions为条件进行查询 |
myContext context = new myContext(); IQueryable<DBItemList> list= context.DBItemList.Where(p => p.ItemValue == 5); |
SELECT [Extent1].[AutoId] AS [AutoId], [Extent1].[NameID] AS [NameID], [Extent1].[ItemID] AS [ItemID], [Extent1].[ItemValue] AS [ItemValue] FROM [dbo].[DBItemList] AS [Extent1] WHERE 5 = [Extent1].[ItemValue] |
过滤集合中的相同项 ObjectQuery<T> Distinct() |
myContext context = new myContext(); ObjectQuery<DbDataRecord> list = context.DBItemList.Select("it.ItemValue"); ObjectQuery<DbDataRecord> dlist= list.Distinct(); |
SELECT [Distinct1].[C1] AS [C1], [Distinct1].[ItemValue] AS [ItemValue] FROM ( SELECT DISTINCT [Extent1].[ItemValue] AS [ItemValue], 1 AS [C1] FROM [dbo].[DBItemList] AS [Extent1] ) AS [Distinct1] |
排序升,排序降 |
myContext context = new myContext(); IQueryable<DBItemList> list = context.DBItemList.OrderBy(p=>p.ItemValue); IQueryable<DBItemList> list = context.DBItemList.OrderByDescending(p=>p.ItemValue); |
SELECT [Extent1].[AutoId] AS [AutoId], [Extent1].[NameID] AS [NameID], [Extent1].[ItemID] AS [ItemID], [Extent1].[ItemValue] AS [ItemValue] FROM [dbo].[DBItemList] AS [Extent1] ORDER BY [Extent1].[ItemValue] ASC |
SELECT [Extent1].[AutoId] AS [AutoId], [Extent1].[NameID] AS [NameID], [Extent1].[ItemID] AS [ItemID], [Extent1].[ItemValue] AS [ItemValue] FROM [dbo].[DBItemList] AS [Extent1] ORDER BY [Extent1].[ItemValue] DESC |
ThenBy,ThenByDescending 方法必须跟在 OrderBy 方法或对 ThenBy 方法的另一次调用之后 当用OrderBy,OrderByDescending指定主排序字段后,可用ThenBy呀ThenByDescending指定次排序字段 |
myContext context = new myContext(); IQueryable<DBItemList> query = context.DBItemList.OrderBy(p=>p.ItemValue).ThenByDescending(p => p.ItemID); foreach (var r in query) { Console.WriteLine("{0},{1},{2},{3}", r.AutoId, r.ItemID, r.NameID, r.ItemValue); } |
SELECT [Extent1].[AutoId] AS [AutoId], [Extent1].[NameID] AS [NameID], [Extent1].[ItemID] AS [ItemID], [Extent1].[ItemValue] AS [ItemValue] FROM [dbo].[DBItemList] AS [Extent1] ORDER BY [Extent1].[ItemValue] ASC, [Extent1].[ItemID] DESC |
平均值,求和 |
myContext context = new myContext(); double d = context.DBItemList.Average(p => p.ItemValue); double s = context.DBItemList.Sum(p => p.ItemValue); |
SELECT [GroupBy1].[A1] AS [C1] FROM ( SELECT cast(1 as bit) AS X ) AS [SingleRowTable1] LEFT OUTER JOIN (SELECT AVG( CAST( [Extent1].[ItemValue] AS float)) AS [A1] FROM [dbo].[DBItemList] AS [Extent1] ) AS [GroupBy1] ON 1 = 1 |
SELECT [GroupBy1].[A1] AS [C1] FROM ( SELECT cast(1 as bit) AS X ) AS [SingleRowTable1] LEFT OUTER JOIN (SELECT SUM([Extent1].[ItemValue]) AS [A1] FROM [dbo].[DBItemList] AS [Extent1] ) AS [GroupBy1] ON 1 = 1 |
集合最大值,最小值 |
myContext context = new myContext(); var mx = context.DBItemList.Max(p => p.ItemValue); var mi = context.DBItemList.Min(p => p.ItemValue); |
SELECT [GroupBy1].[A1] AS [C1] FROM ( SELECT cast(1 as bit) AS X ) AS [SingleRowTable1] LEFT OUTER JOIN (SELECT MAX([Extent1].[ItemValue]) AS [A1] FROM [dbo].[DBItemList] AS [Extent1] ) AS [GroupBy1] ON 1 = 1 |
SELECT [GroupBy1].[A1] AS [C1] FROM ( SELECT cast(1 as bit) AS X</s 发表评论 |
评论