3

私たちのデータモデルには、階層データがあります。たとえば、次のような構造があります。

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

1 に答える 1

5

型なし、型付き、および動的バージョンの両方を含む 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);
}
于 2015-03-27T08:42:29.133 に答える