ODataComplexValue操作和转换

时间:2015-11-15 13:56:12

标签: c# odata

所以我是OData的新手,我使用Simple.OData.Client

所以我的代码首先看起来像这样:

var context = new ODataClient("http://localhost:51861/API/");  
var test = context.For<Account>()
            .Key("00010017")
            .Action("Testing")
            .Set(new Entry() { { "MyTest",  New Test() { Item = "Hello World"}  } })
            .ExecuteAsScalarAsync<Test>()
            .Result;

导致以下错误:

"The value for parameter 'MyTest' is of type 'ODataExample.Model.MyTest'. WriteValue can only write null, ODataComplexValue, ODataEnumValue and primitive types that are not Stream type."

好的,很酷,所以上面的例外是有用的,我可以通过

来解决这个问题
 ODataComplexValue MyTest = new ODataComplexValue();
 ODataProperty myP = new ODataProperty();

 myP.Name = "Item";
 myP.Value = "Hello World";

 myCustomer.TypeName = "ODataExample.Model.MyTest";
 myCustomer.Properties = new[] { myP };

现在,如果我将myTest传递给.Set,我可以调用我的OData服务器

  .Set(new Entry() { { "MyTest",  MyTest )

所以显然我可以用

之类的东西创建一个ODataComplexValue
 var tt = new ODataComplexValue()
            {
                TypeName = t.GetType().FullName,
                Properties =  s.GetType().GetProperties().Select<PropertyInfo,ODataProperty>(x => new ODataProperty { Name = x.Name, Value = x.GetValue(t) })
            };

以上作品很棒,我只是创建了一个扩展方法,以便

.Set(new Entry() { { "MyTest",  New Test() { Item = "Hello World"}  } }.ToOData())

然而,我无法帮助,但我觉得我错过了一些东西,如果解决方案很简单,为什么Simple.OData.Client不仅仅是为我做这个?是否已经存在我应该使用的扩展方法或实用程序来获得相同的上述结果。

如果需要我的webapi路由,请按照以下步骤操作:

   var function = builder.EntityType<Account>().Action("Testing");
            function.Namespace = "Transaction";
            function.Parameter<Test>("MyTest");
            function.ReturnsFromEntitySet<Test>("Test");

0 个答案:

没有答案