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

时间:2015-03-23 09:43:47

标签: c# odata-v4 simple.odata

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

Product  - Category (on to many)
Category - CategoryType   (on to many)

如果我们想要某个产品的相关类别以及每个类别的相关类别类型,我们可以使用以下网址构建此类:

<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();

1 个答案:

答案 0 :(得分:4)

请检查包含无类型,类型和动态版本的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);
}