ObjectQuery<T>数据加载方式
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<实体>
访问方式
Context.CreateQuery<T>()
string econString = @" metadata=res:/myModel.ssdl |res:/myModel.csdl |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); } |
ObjectQuery<DbDataRecord>
string econString = @" metadata=res:/myModel.ssdl |res:/myModel.csdl |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() ); } |
Execute方法与ObjectResult
Execute方法
string econString = @" metadata=res:/myModel.ssdl |res:/ |
SELECT [Project2].[ItemID] AS [ItemID], [Project2].[C1] AS [C1], [Project2].[C2] AS [C2], [Project2].[AutoId] AS [AutoId], [Project2].[NameID] AS [NameID], [Project2].[ItemID1] AS [ItemID1], [Project2].[ItemValue] AS [ItemValue] FROM ( SELECT [Distinct1].[ItemID] AS [ItemID], 1 AS [C1], [Extent2].[AutoId] AS [AutoId], [Extent2].[NameID] AS [NameID], [Extent2].[ItemID] AS [ItemID1], [Extent2].[ItemValue] AS [ItemValue], CASE WHEN ([Extent2].[AutoId] IS NULL) THEN CAST(NULL AS int) ELSE1 END AS [C2] FROM (SELECT DISTINCT [Extent1].[ItemID] AS [ItemID] FROM [dbo].[DBItemList] AS [Extent1] ) AS [Distinct1] LEFT OUTER JOIN [dbo].[DBItemList] AS [Extent2] ON([Extent2].[ItemID] = [Distinct1].[ItemID]) OR(([Extent2].[ItemID] ISNULL) AND ([Distinct1].[ItemID] IS NULL)) ) AS [Project2] ORDER BY [Project2].[ItemID] ASC, [Project2].[C2] ASC |
Join
联合查询 |
myContext context1 = new myContext(); ObjectQuery<DBItem> query1 = context1.DBItem; ObjectQuery<DBItemList> query2 = context1.DBItemList; var v = query1.Join(query2, temp1 => temp1.ItemID, temp2 => temp2.ItemID, (temp1, temp2) => new{ temp1.ItemID, temp1.ItemMatter, temp2.ItemValue, temp2.AutoId, temp2.NameID }); foreach (var r in v) { Console.WriteLine("{0},{1},{2},{3},{4}", r.AutoId, r.ItemID, r.NameID, r.ItemValue,r.ItemMatter); } |
myContext context1 = new myContext(); ObjectQuery<DBItem> query1 = context1.DBItem; ObjectQuery<DBItemList> query2 = context1.DBItemList; var v = from temp1 in query1 join temp2 in query2 on temp1.ItemID equals temp2.ItemID select new { temp1.ItemID, temp1.ItemMatter, temp2.ItemValue, temp2.AutoId,temp2.NameID }; foreach (var r in v) { Console.WriteLine("{0},{1},{2},{3},{4}", r.AutoId, r.ItemID, r.NameID, r.ItemValue,r.ItemMatter); } |
SELECT 1 AS [C1], [Extent1].[ItemID] AS [ItemID], [Extent1].[ItemMatter] AS [ItemMatter], [Extent2].[ItemValue] AS [ItemValue], [Extent2].[AutoId] AS [AutoId], [Extent2].[NameID] AS [NameID] FROM [dbo].[DBItem] AS [Extent1] INNER JOIN [dbo].[DBItemList] AS [Extent2] ON ([Extent1].[ItemID] =[Extent2].[ItemID]) OR(([Extent1].[ItemID] IS NULL) AND ([Extent2].[ItemID] IS NULL)) |
GroupJoin
myContext context1 = new myContext(); ObjectQuery<DBItem> query1 = context1.DBItem; ObjectQuery<DBItemList> query2 = context1.DBItemList; var v = query1.GroupJoin(query2, temp1 => temp1.ItemID, temp2 => temp2.ItemID, (temp1, temp2) =>new { temp1.ItemID, temp1.ItemMatter, 个数 = temp2.Count() }); foreach (var r in v) { Console.WriteLine("{0},{1},{2}", r.ItemID, r.ItemMatter,r.个数); } |
myContext context1 = new myContext(); ObjectQuery<DBItem> query1 = context1.DBItem; ObjectQuery<DBItemList> query2 = context1.DBItemList; var v = from temp1 in query1 join temp2 in query2 on temp1.ItemID equals temp2.ItemID into newtab select new { temp1.ItemID, temp1.ItemMatter, 个数 = newtab.Count() }; foreach (var r in v) { Console.WriteLine("{0},{1},{2}", r.ItemID, r.ItemMatter,r.个数); } |
SELECT 1 AS [C1], [Project1].[ItemID] AS [ItemID], [Project1].[ItemMatter] AS [ItemMatter], [Project1].[C1] AS [C2] FROM ( SELECT [Extent1].[ItemID] AS [ItemID], [Extent1].[ItemMatter] AS [ItemMatter], (SELECT COUNT(cast(1 as bit)) AS [A1] FROM [dbo].[DBItemList] AS [Extent2] WHERE ([Extent1].[ItemID] = [Extent2].[ItemID]) OR (([Extent1].[ItemID] IS NULL) AND ([Extent2].[ItemID]IS NULL))) AS[C1] FROM [dbo].[DBItem] AS [Extent1] ) AS [Project1] |
无效的Linq方法
可将ObjectQuery<T>转换为List<T>后使用 List的对应方法
Aggregate
据输入的表达式获取一个聚合值 |
myContext context = new myContext(); List<DBItemList> list = context.DBItemList.ToList(); DBItemList r = list.Aggregate((x, y) => new DBItemList() { ItemValue = x.ItemValue + y.ItemValue }); Console.WriteLine("ItemValue合计为:{0}", r.ItemValue); //print: ItemValue合计为:54 |
TakeWhile
条件第一次不成立就跳出循环 |
myContext context = new myContext(); List<DBItemList> list = context.DBItemList.ToList(); IEnumerable<DBItemList> v = list.TakeWhile(p => p.ItemValue >= 2); foreach (var r in v) { Console.WriteLine(r.ItemValue); } |
SkipWhile
条件第一次不成立就失效,将后面的数据全取 |
myContext context = new myContext(); List<DBItemList> list = context.DBItemList.ToList(); IEnumerable<DBItemList> v = list.SkipWhile(p => p.ItemValue >= 2); foreach (var r in v) { Console.WriteLine(r.ItemValue); } |
Reverse
顺序返转 |
myContext context = new myContext(); IEnumerable<DBItemList> list = context.DBItemList.AsEnumerable(); IEnumerable<DBItemList> v = list.Reverse(); |
Last,LastOrDefault
集合的最后一个元素,集合中没有会报错, 集合中的最后一个元素,没有则返回类型默认值,对象型默认值为null |
myContext context = new myContext(); List<DBItemList> list = context.DBItemList.ToList(); DBItemList l1 = list.Last(); DBItemList li = list.Last(p=>p.ItemValue==5); DBItemList lid = list.LastOrDefault(p => p.ItemValue == 15); if (lid != null) { Console.WriteLine(lid.ItemValue); } |
Single,SingleOrDefault
集合中符合条件的唯一元素,集合中没有会报错,集合中有两个以上会报错 集合中符合条件的唯一元素,集合中有两个以上会报错,集合中没有则返回类型默认值,对象型默认值为null |
myContext context = new myContext(); List<DBItemList> list = context.DBItemList.ToList(); DBItemList di = list.Single(p=>p.ItemValue==5); DBItemList did = list.SingleOrDefault(p => p.ItemValue == 15); if (did != null) { Console.WriteLine(did.ItemValue); } |
Contains
判断集合中是否包含有某一元素 |
myContext context = new myContext(); List<DBItemList> list = context.DBItemList.ToList(); DBItemList r = new DBItemList(); r.ItemValue = 3; bool b = list.Contains(r,new c()); |
class c : IEqualityComparer<DBItemList> { public bool Equals(DBItemList x, DBItemList y) { if (x.ItemValue == y.ItemValue) { return true; } else { return false; } } public int GetHashCode(DBItemList obj) { return 0; } } |
Distinct
过滤集合中的相同项 |
myContext context = new myContext(); List<DBItemList> list = context.DBItemList.ToList(); IEnumerable<DBItemList> v = list.Distinct(new c()); |
class c : IEqualityComparer<DBItemList> { public bool Equals(DBItemList x, DBItemList y) { if (x.ItemValue == y.ItemValue) { return true; } else { return false; } } public int GetHashCode(DBItemList obj) { return 0; } } |
ElementAt,ElementAtOrDefault
集合中指定索引的元素 集合中指定索引的元素,没有则返回类型默认值,对象型默认值为null |
myContext context = new myContext(); List<DBItemList> list = context.DBItemList.ToList(); DBItemList r = list.ElementAt(5); DBItemList rd = list.ElementAtOrDefault(50); |
DefaultIfEmpty
如果集合是的无素为空(count==0),就向集合中插入一个默认元素 |
DBItemList r=new DBItemList(); r.ItemValue=100; List<DBItemList> list = new List<DBItemList>(); var pp = list.DefaultIfEmpty(r); |
SelectMany
myContext context = new myContext(); List<DBItemList> query = context.DBItemList.ToList(); IEnumerable<string> ls=query.SelectMany(p => p.NameID.Split('0')); foreach (string r in ls) { Console.WriteLine(r); } |
|
Lambda表达式 |
List<string> ls = new List<string>() { "wxd/1", "lzm/2", "wxwinter/3" }; var li = ls.SelectMany(p => p.Split('/')); foreach (var s in li) { Console.WriteLine(s); } |
对应Select效果 |
var ll = ls.Select(p => p.Split('/')); foreach (var s in ll) { foreach (var ss in s) { Console.WriteLine(ss); } } |
SequenceEqual
myContext context = new myContext(); |