Linq结果集形状概述

开发 后端
这里介绍返回的Linq结果集形状取决于传入的参数。如果参数等于 1,则返回所有客户属性。如果参数等于2,则返回ContactName属性。

Linq有很多值得学习的地方,这里我们主要介绍Linq结果集形状,包括介绍 ResultTypeAttribute 属性适用于返回多个结果类型的存储过程等方面。

当存储过程可以返回多个Linq结果集形状时,返回类型无法强类型化为单个投影形状。尽管 LINQ to SQL 可以生成所有可能的投影类型,但它无法获知将以何种顺序返回它们。 ResultTypeAttribute 属性适用于返回多个结果类型的存储过程,用以指定该过程可以返回的类型的集合。

在下面的 SQL 代码示例中,Linq结果集形状取决于输入(param1 = 1或param1 = 2)。我们不知道先返回哪个投影。

  1. ALTER PROCEDURE [dbo].[SingleRowset_MultiShape]  
  2. -- Add the parameters for the stored procedure here  
  3. (@param1 int )  
  4. AS  
  5. BEGIN  
  6. -- SET NOCOUNT ON added to prevent extra result sets from  
  7. -- interfering with SELECT statements.  
  8. SET NOCOUNT ON;  
  9. if(@param1 = 1)  
  10. SELECT * from Customers as c where c.Region = 'WA' 
  11. else if (@param1 = 2)  
  12. SELECT CustomerID, ContactName, CompanyName from   
  13. Customers as c where c.Region = 'WA' 
  14. END 

拖到O/R设计器内,它自动生成了以下代码段:

  1. [Function(Name="dbo.[Whole Or Partial Customers Set]")]  
  2. public ISingleResult<Whole_Or_Partial_Customers_SetResult>   
  3. Whole_Or_Partial_Customers_Set([Parameter(DbType="Int")]   
  4. System.Nullable<int> param1)  
  5. {  
  6. IExecuteResult result = this.ExecuteMethodCall(this,   
  7. ((MethodInfo)(MethodInfo.GetCurrentMethod())), param1);  
  8. return ((ISingleResult<Whole_Or_Partial_Customers_SetResult>)  
  9. (result.ReturnValue));  

但是,VS2008会把多结果集存储过程识别为单结果集的存储过程,默认生成的代码我们要手动修改一下,要求返回多个结果集,像这样:

  1. [Function(Name="dbo.[Whole Or Partial Customers Set]")]  
  2. [ResultType(typeof(WholeCustomersSetResult))]  
  3. [ResultType(typeof(PartialCustomersSetResult))]  
  4. public IMultipleResults Whole_Or_Partial_Customers_Set([Parameter  
  5. (DbType="Int")] System.Nullable<int> param1)  
  6. {  
  7. IExecuteResult result = this.ExecuteMethodCall(this,   
  8. ((MethodInfo)(MethodInfo.GetCurrentMethod())), param1);  
  9. return ((IMultipleResults)(result.ReturnValue));  

我们分别定义了两个分部类,用于指定返回的类型。这样就可以使用了,下面代码直接调用,分别返回各自的结果集合。

  1. //返回全部Customer结果集  
  2. IMultipleResults result = db.Whole_Or_Partial_Customers_Set(1);  
  3. IEnumerable<WholeCustomersSetResult> shape1 =  
  4. result.GetResult<WholeCustomersSetResult>();  
  5. foreach (WholeCustomersSetResult compName in shape1)  
  6. {  
  7. Console.WriteLine(compName.CompanyName);  
  8. }  
  9. //返回部分Customer结果集  
  10. result = db.Whole_Or_Partial_Customers_Set(2);  
  11. IEnumerable<PartialCustomersSetResult> shape2 =  
  12. result.GetResult<PartialCustomersSetResult>();  
  13. foreach (PartialCustomersSetResult con in shape2)  
  14. {  
  15. Console.WriteLine(con.ContactName);  

语句描述:这个实例使用存储过程返回“WA”地区中的一组客户。返回的Linq结果集形状取决于传入的参数。如果参数等于 1,则返回所有客户属性。如果参数等于2,则返回ContactName属性。

【编辑推荐】

  1. LINQ to DataSet查询详解
  2. Linq实现XML转换浅谈
  3. Linq to SQL强类型DataContext
  4. Linq SelectMany学习经验
  5. 使用LINQ进行数据转换剖析
责任编辑:佚名 来源: CSDN
相关推荐

2009-09-09 11:14:04

Linq多个结果集

2009-09-15 10:34:32

System.Data

2009-09-15 15:45:00

Linq联合查询

2009-09-10 13:42:47

Linq UserIn

2009-09-10 11:10:21

Linq Librar

2009-09-17 10:27:55

linq存储过程

2009-09-18 16:20:36

LINQ基础

2009-09-08 11:25:42

Linq foreac

2009-09-08 16:08:44

Linq使用order

2009-09-18 16:07:10

Linq Where操

2009-09-14 09:55:55

Linq基本语法

2009-09-10 16:28:17

LINQ查询

2009-09-18 13:44:38

LINQ设计模式

2009-09-14 15:15:45

LINQ技术

2009-09-16 17:21:53

LINQ遍历

2009-09-11 12:13:40

LINQ to SQL

2009-09-14 10:09:26

LINQ查询结果

2009-09-09 16:07:16

Linq实体关系

2009-09-09 16:01:21

Linq实体继承使用

2009-09-11 10:16:07

Linq匿名类型
点赞
收藏

51CTO技术栈公众号