Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Odata中集合的更改序列化_C#_Json.net_Odata_Asp.net Web Api_Asp.net Web Api2 - Fatal编程技术网

C# Odata中集合的更改序列化

C# Odata中集合的更改序列化,c#,json.net,odata,asp.net-web-api,asp.net-web-api2,C#,Json.net,Odata,Asp.net Web Api,Asp.net Web Api2,我有以下目标: public class Car { public virtual int Id {get;set;} public virtual IList<Spec> Specs { get; set; } public virtual IList<Owner> Owners { get; set; } } public class Spec { public virtual int Id { get; set;} public v

我有以下目标:

public class Car 
{
   public virtual int Id {get;set;}
   public virtual IList<Spec> Specs { get; set; }
   public virtual IList<Owner> Owners { get; set; }
}

public class Spec
{
   public virtual int Id { get; set;}
   public virtual string Info { get; set;}
}

 public class Owner: ILast
{
   public virtual int Id { get; set;}
   public virtual string FullName{ get; set;}
   public virtual DateTime DateTime { get; set;}
}
如果查询实现ILast接口,则该查询将返回IQuerable和have谓词,它们选择具有last DateTime属性的实体。例如,所有者表中有以下数据:

所有者表:

id | FullName | DateTime | CarId
1      name1    12.01.13   1
2      name2    15.04.15   1
查询将只返回第二个

在JSON结果中,我假设Spec表也有2条记录:

Car = {
  id: 1,
  Specs: {
     [0]: object with spec properties,
     [1]: object with spec properties,   
  },
  Owners: {
     [0]: {
         id: 1,
         FullName: 'name2',
         DateTime: '15.04.5'
      }
  }
}
但是,我想更改序列化,Owners属性始终包含一条记录。我希望获得以下JSON:

Car = {
      id: 1,
      Specs: {
         [0]: object with spec properties,
         [1]: object with spec properties,   
      },
      Owners: {
         id: 1,
         FullName: 'name2',
         DateTime: '15.04,.5'
      }
    }
我们可以通过接口来确定实体


另外,我用的是小田

您可以通过编写自定义序列化来实现这一点,但我不建议使用单个对象覆盖数组项表示,因为那样就不是标准


我建议在汽车对象中创建两个属性,一个所有者和另一个过去的所有者,不要使用JSON ignore向用户公开过去的所有者。

这只是一个属性DateTime的接口如果汽车只有一个所有者,那么为什么要将所有者作为其属性。保留它只是所有者,而不是所有者list@Guanxi:因为可能有很多记录。但用户必须只看到一个。它就像历史实体。我使用OData进行数据访问。
Car = {
      id: 1,
      Specs: {
         [0]: object with spec properties,
         [1]: object with spec properties,   
      },
      Owners: {
         id: 1,
         FullName: 'name2',
         DateTime: '15.04,.5'
      }
    }