$count(RowCount):只回傳實體集合筆數。SetEntitySetPageSize 限制每頁筆數,配合 $skip/$inlinecount。$select:投影指定欄位,減少傳輸量。IDataServiceStreamProvider。IQueryable 暴露記憶體集合。public class MyDataService : DataService<AdventureWorksEntities>
{
public static void InitializeService(DataServiceConfiguration config)
{
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
config.SetEntitySetAccessRule("Products", EntitySetRights.All);
config.SetEntitySetPageSize("Products", 5); // 分頁每頁 5 筆
}
}
// 用戶端
var count = context.Products.Count(); // 對應 $count
// URI: /Products/$count
// 分頁含總筆數
var products = context.Products.Skip(10); // $skip=10
// URI: /Products?$skip=10&$inlinecount=allpages
服務端 SetEntitySetPageSize("Products", 5),用戶端用 Skip() 翻頁,回傳內含 __count 欄位(若加 $inlinecount=allpages)。
var products = context.Products.Select(p => new { p.ProductID, p.Name });
// URI: /Products?$select=ProductID,Name
m:HasStream="true"。IServiceProvider,GetService 回傳自訂 IDataServiceStreamProvider。GetReadStream 回傳 MemoryStream,GetStreamContentType 回傳 MIME。public class ProductPhotoStreamProvider : IDataServiceStreamProvider
{
public Stream GetReadStream(object entity, ...)
{
var product = (Product)entity;
return new MemoryStream(product.ThumbNailPhoto);
}
public string GetStreamContentType(object entity, ...) => "image/jpeg";
public int StreamBufferSize => 64;
// 其他成員拋 NotImplementedException
}
前端直接用 <img src="/Products(714)/$value" /> 顯示圖片。
繼承 DataService<CustomContext>,CustomContext 暴露 IQueryable<T> 屬性。實體類別加 [DataServiceKey] 指定主鍵,[EntityPropertyMapping] 映射 ATOM 欄位。
[DataServiceKey("OrderId")]
[EntityPropertyMapping("Customer", SyndicationItemProperty.AuthorName, SyndicationTextContentKind.Plaintext, true)]
public class Order { public int OrderId { get; set; } public string Customer { get; set; } public List<Product> Items { get; set; } }
https://[Url]/[ServiceName]/[EntityName]/[NavigationOptions]?[QueryOptions]
$count、$inlinecount=none/allpages$skip=n、$top=n$select=col1,col2$filter=...、$orderby=...、$expand=...Data Services 1.5 大幅強化了 OData 協定支援,特別是分頁、投影、計數與串流,讓 RESTful 資料服務更實用、效能更佳。