c# - 如何使用 simple.odata.client 扩展分层数据

标签 c# odata simple.odata.client

在我们的数据模型中,我们有分层数据。例如,我们有以下结构:

Product  : Category       (one to many)
Category : CategoryType   (one to many)

如果我们想要某个产品的相关类别以及每个类别的相关类别类型,我们可以使用这样的 url 来构建它:

<urlbase>/Products(1)?$expand=Category($expand=CategoryType)

这在浏览器中运行良好。问题是这如何在 simple.odata.client OData v4 中完成?我们正在尝试这个,但没有让它发挥作用:

var client = new ODataClient(ConfigSettingsProvider.ODataBaseUri);
var client
    .For<Product>()
    .Filter(p=> p.Id == 1)
    .Expand(p => p.Categories)
    .Expand(c => c.CategoryTypes)
    .FindEntriesAsync();

最佳答案

请检查包含非类型化、类型化和动态版本的 Simple.OData.Client 测试:

未键入:https://github.com/object/Simple.OData.Client/blob/master/Simple.OData.Client.Tests.Net40/FindTests.cs

[Fact]
public async Task ExpandOne()
{
var product = (await _client
  .For("Products")
  .OrderBy("ProductID")
  .Expand("Category")
  .FindEntriesAsync()).Last();

Assert.Equal("Condiments", (product["Category"] as IDictionary<string, object>)["CategoryName"]);
}
[Fact]
public async Task ExpandMany()
{
var category = await _client
  .For("Categories")
  .Expand("Products")
  .Filter("CategoryName eq 'Beverages'")
  .FindEntryAsync();

Assert.Equal(12, (category["Products"] as IEnumerable<object>).Count());
}
[Fact]
public async Task ExpandSecondLevel()
{
var product = (await _client
  .For("Products")
  .OrderBy("ProductID")
  .Expand("Category/Products")
  .FindEntriesAsync()).Last();

Assert.Equal(12, ((product["Category"] as IDictionary<string, object>)["Products"] as IEnumerable<object>).Count());
}

键入:https://github.com/object/Simple.OData.Client/blob/master/Simple.OData.Client.Tests.Net40/FindTypedTests.cs

 [Fact]
public async Task ExpandOne()
{
var product = (await _client
  .For<Product>()
  .OrderBy(x => x.ProductID)
  .Expand(x => x.Category)
  .FindEntriesAsync()).Last();

Assert.Equal("Condiments", product.Category.CategoryName);
}
[Fact]
public async Task ExpandManyAsArray()
{
var category = await _client
  .For<Category>()
  .Expand(x => x.Products)
  .Filter(x => x.CategoryName == "Beverages")
  .FindEntryAsync();

Assert.Equal(12, category.Products.Count());
}
[Fact]
public async Task ExpandManyAsList()
{
var category = await _client
  .For<CategoryWithList>("Categories")
  .Expand(x => x.Products)
  .Filter(x => x.CategoryName == "Beverages")
  .FindEntryAsync();

Assert.Equal(12, category.Products.Count());
}
[Fact]
public async Task ExpandManyAsIList()
{
var category = await _client
  .For<CategoryWithIList>("Categories")
  .Expand(x => x.Products)
  .Filter(x => x.CategoryName == "Beverages")
  .FindEntryAsync();

Assert.Equal(12, category.Products.Count());
}
[Fact]
public async Task ExpandManyAsICollection()
{
var category = await _client
  .For<CategoryWithICollection>("Categories")
  .Expand(x => x.Products)
  .Filter(x => x.CategoryName == "Beverages")
  .FindEntryAsync();

Assert.Equal(12, category.Products.Count());
}
[Fact]
public async Task ExpandSecondLevel()
{
var product = (await _client
  .For<Product>()
  .OrderBy(x => x.ProductID)
  .Expand(x => x.Category.Products)
  .FindEntriesAsync()).Last();

Assert.Equal(12, product.Category.Products.Length);
}

关于c# - 如何使用 simple.odata.client 扩展分层数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29207080/

相关文章:

c# - 在 Asp.Net MVC 项目中托管 WCF 服务

c# - C# 的 mysql 存储过程出错

asp.net - 抽象通用 ODataController 类导致 'No HTTP resource was found'

javascript - 将图像上传到 SAP OData 服务时出现问题 - Put 方法

javascript - Kendo 数据源 shema.data 不适用于下拉列表

c# - 如何更新远程 ms sql server 上的数据库(EF 代码优先)

c# - 在 JS 和 C# 中通过 POST 请求解析 JSON 数据

dynamics-crm - CRM 不支持直接更新实体引用属性,请改用导航属性

c# - 简单 Odata 客户端 - 如何在每个请求 header 中添加 oAuth token ?