【问题标题】:Massive with WCF Web Api to return dynamic types/Expandos?使用 WCF Web Api 大量返回动态类型/Expandos?
【发布时间】:2011-12-19 16:23:51
【问题描述】:

我想通过 WCF Web Api 使用 Massive 进行数据访问,并从我的 Web api 返回动态或 ExpandoObject / IEnumerable

我基本上使用 JsonNetMediaTypeFormatter 工作,它使用 Json.NET 的 ExpandoObject 序列化,但所有内容都作为 Json 中的键值对返回,例如:

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

但是,我想要的是:

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

好像我使用的是具体类型,例如:

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

关于从 WCF Web Api 返回时如何将动态/ExpandoObject 格式化为具体对象的任何想法?

【问题讨论】:

  • 没关系,我通过清除现有的格式化程序集合让它工作了。我以为我已经这样做了,但是我设置了一个断点以查看它是否正在访问我的 JsonNetMediaTypeFormatter,但事实并非如此。又试了一次,现在可以了。
  • 你能发布你正在使用的代码吗?
  • 是的,我也可以有一份副本吗...我现在正在尝试这样做

标签: .net c#-4.0 dynamic wcf-web-api massive


【解决方案1】:

【讨论】:

    【解决方案2】:

    我认为您正在使用 Expando Query 并将其传递给 WCF。只需尝试进行迭代或将 ToList 提供给您的集合。这会将 ExpandoQuery 转换为 Expando 对象集合。如果你是 POCO 来映射,因为客户在你的问题中。使用您的 POCO 对象进行选择。

    如果您的查询是

    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.
    

    我希望这会对你有所帮助。让我知道是否还有其他内容。

    【讨论】:

      猜你喜欢
      • 2012-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多