神仙?妖怪?谢谢!

Just do it...

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

演练:添加查询方法
查询数据源的方法有时被叫做查询方法。在WCF RIA Services中,查询方法必须以框架承认的方式来定义。此外,只返回一个实体的查询和有可能返回多个实体的查询定义是不同的。
当我们建立一个新的domain service类并在Add New Domain Service Class 对话框中指定实体时,RIA Services框架会自动为每一个服务端公开的实体创建一个简单的查询。这个简单的查询方法检索实体的所有数据。这个演练将描述如何添加一个用参数值来过滤结果的复杂查询方法。还描述了如何添加一个返回单个实体和一个实体集合的查询。


添加一个接受参数并返回单一实体的查询方法

  1. 打开我们中创建的RIAServicesExample解决方案。
  2. 在服务端,打开从Customer表公开数据的 domain Services 类。这个类应该叫做CustmerDomainService。
  3. 添加一个查询方法,这个方法接受一个整数类型的参数并返回符合Customer ID的Customer实体。 如果返回单一实体的方法包含Query属性,必须设置IsComposable为false. 用户不能从客户端指定其他的查询操作。如果这个查询方法满足了作为查询所期望的签名,我们就不必使用[Query]属性。返回值必须是任何实体对象的单一实例。
1 [Query(IsComposable=false)]    
2 public Customer GetCustomersByID(int customerID) { return this.ObjectContext.Customers.FirstOrDefault(c => c.CustomerID == customerID);   
3  } 

 

 

添加一个接受一个参数并返回一个实体集合的查询方法

  1. 打开从Customer表公开数据的domain service类。名字应为CustomerDomainService。
  2. 添加一个方法,这个方法接受一个字符型参数并返回所有名字以参数开始的客户。这个方法可以返回一个IQueryable<>对象,因为用户可能想从客户端提供额外的查询。  
代码
1 public IQueryable<customer> GetCustomersByLastNameLetter(string startingLastNameLetter)   
2 {   
3     return this.ObjectContext.Customers.Where(c => c.LastName.StartsWith(startingLastNameLetter) == true);   
4 }  

 

 
在客户端显示这些查询的结果

  1. 在客户端打开MainPage.xaml文件。
  2. 添加两个TextBox控件和两个Button控件,这样用过就可以通过ID或名的首字母来过滤。下面的xaml代码显示了DataGrid的完整布局。
代码
 1 <usercontrol class="RIAServicesExample.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" designheight="300" designwidth="400" ignorable="d" mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d="http://schemas.microsoft.com/expression/blend/2008" x="http://schemas.microsoft.com/winfx/2006/xaml" data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data">  
 2     <grid name="LayoutRoot" background="White">  
 3         <grid.columndefinitions>  
 4             <columndefinition></columndefinition>  
 5             <columndefinition></columndefinition>  
 6         </grid.columndefinitions>  
 7         <grid.rowdefinitions>  
 8             <rowdefinition height="25"></rowdefinition>  
 9             <rowdefinition></rowdefinition>  
10         </grid.rowdefinitions>  
11         <stackpanel orientation="Horizontal" row="0" column="0">  
12             <textblock text="search by id: "></textblock>  
13             <textbox name="IDValue" width="50"></textbox>  
14             <button name="IDButton" content="Submit" click="IDButton_Click"></button>  
15         </stackpanel>  
16         <stackpanel orientation="Horizontal" row="0" column="1">  
17             <textblock text="search by name: "></textblock>  
18             <textbox name="LetterValue" width="30"></textbox>  
19             <button name="LetterButton" content="Submit" click="LetterButton_Click"></button>  
20         </stackpanel>  
21       <data:datagrid name="CustomerGrid" row="1" column="0" columnspan="2"></data:datagrid>  
22     </grid>  
23 </usercontrol>

   3.  打开MainPage.xaml的代码文件。 
   4.  添加代码来根据用户的输入来检索数据。

[Query(IsComposable=false)]   
public Customer GetCustomersByID(int customerID)   
{   
    
return this.ObjectContext.Customers.FirstOrDefault(c => c.CustomerID == customerID);   
}  

 

   5.  运行解决方案。将会看到如下结果

  

 


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/blackant2/archive/2010/04/08/5461360.aspx

posted on 2010-06-05 00:53  E.Trock  阅读(1553)  评论(0编辑  收藏  举报