本文介绍了大规模使用WCF的Web API来返回动态类型/ expandos的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用大规模与WCF的Web API访问数据并返回动态或ExpandoObject / IEnumerable的< ExpandoObject>从我的Web API。

I want to use Massive for data access with WCF Web Api and return either dynamic or ExpandoObject / IEnumerable<ExpandoObject> from my web api.

我有这个工作基本上使用JsonNetMediaTypeFormatter它使用Json.NET的ExpandoObject系列化,但一切都被返回在JSON一个键值对,例如:

I have this basically working using a JsonNetMediaTypeFormatter which uses Json.NET's ExpandoObject serialization, but everything gets returned as a Key-Value pairs in the Json such as:

[
    {
        "Key":"ID",
        "Value":"1000"
    },
    {
        "Key":"FirstName",
        "Value":"John"
    },
    {
        "Key":"LastName",
        "Value":"Smith"
    }
]

不过,我要的是:

But, what I want is:

[
    {
        "ID":"1000",
        "FirstName":"John",
        "LastName":"Smith",
    }
]

就好像我是用一个具体的类型,如:

As if I were using a concrete type like:

public class Customer
{
    public int ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

如何从WCF的Web API返回的时候,获取动态/ ExpandoObject格式化像一个具体的对象,任何想法?

Any ideas on how to get the dynamic/ExpandoObject formatted like a concrete object when returned from WCF Web Api?

推荐答案

我认为你正在服用Expando的查询​​,并传递到WCF。刚刚尝试做迭代或者只给了ToList到您的收藏。这将转换ExpandoQuery到EXPANDO对象集合。而如果你是POCO映射,因为客户有没有喜欢你的问题。举一个选择你的POCO的对象。

I think you are taking Expando Query and passing to WCF. Just try to do iteration or just give ToList to your collection. That will convert ExpandoQuery to Expando Object Collection. And if you are POCO to map, as Customer is there like in your question. Give a Select with your POCO objects.

一样,如果你的查询是

Dynamic CustomerTable = DynamicObject("ConnectionString","TableName","PrimeryKey");

CustomerTable.All() //This will be ExpandoQuery

CustomerTable.All().Select(c=> new Customer () {FistName = c.FirstName, LastName = c.LastName}); // This will give collection of customer object. Just pass this as DTO to your WCF service.

我希望这会帮助你。让我知道,如果有什么进一步的存在。

I hope this will help you. Let me know if anything further is there.

这篇关于大规模使用WCF的Web API来返回动态类型/ expandos的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 23:53