C# 作为IEnumerable返回对象的锯齿数组
我正在使用ASP.NET API,从基类检索不同派生对象的锯齿数组时遇到了一些问题。 所以我有一个基类:C# 作为IEnumerable返回对象的锯齿数组,c#,asp.net-web-api,ienumerable,C#,Asp.net Web Api,Ienumerable,我正在使用ASP.NET API,从基类检索不同派生对象的锯齿数组时遇到了一些问题。 所以我有一个基类: public class BaseContract { public int ID { get; set; } public enum ContractTypes { A, B } public ContractTypes contractType; public string provid
public class BaseContract
{
public int ID { get; set; }
public enum ContractTypes
{
A,
B
}
public ContractTypes contractType;
public string provider { get; set; }
public string user { get; set; }
public enum OfficeLocations
{
A,
B,
C
}
public OfficeLocations office_location;
public DateTime startDate { get; set; }
public DateTime finishDate { get; set; }
}
然后再从中派生出4个类:
public class class1 : BaseContract
{
public enum ProductTypes {
A,
B,
C
}
public ProductTypes productType;
}
像这样我有4节课
在开始从数据库获取数据之前,我将手动使用一些随机数据填充这些类:
public class ContractsController : ApiController
{
BaseContract[][] contracts = new BaseContract[][]{
new class1[]
{
new class1 {
ID=12412341,
contractType = BaseContract.ContractTypes.A,
productType= Protection_automobile.ProductTypes.B,
startDate= new DateTime(2014, 3, 15),
finishDate= new DateTime(2014, 4, 11),
office_location=BaseContract.OfficeLocations.A,
provider="abc"},
new class1 {
ID=52412341,
contractType = BaseContract.ContractTypes.B,
productType= Protection_automobile.ProductTypes.C,
startDate= new DateTime(2015, 7, 24),
finishDate= new DateTime(2015, 9, 2),
office_location=BaseContract.OfficeLocations.C,
provider="wer"}
},
new class2[]
{
new class2{
ID=22374341,
contractType= BaseContract.ContractTypes.A,
productType = Protection_biens.ProductTypes.B,
startDate = new DateTime(2015,2,2),
finishDate =new DateTime(2015,4,17),
office_location = BaseContract.OfficeLocations.B,
provider= "wer"
},
new class2{
ID=12374659,
contractType = BaseContract.ContractTypes.A,
productType = Protection_biens.ProductTypes.A,
startDate = new DateTime(2015,7,1),
finishDate = new DateTime(2015, 5,30),
office_location = BaseContract.OfficeLocations.A,
provider = "abc"
},
new class2{
ID=12374659,
contractType = BaseContract.ContractTypes.B,
productType = Protection_biens.ProductTypes.A,
startDate = new DateTime(2015,8,2),
finishDate = new DateTime(2015, 5,30),
office_location = BaseContract.OfficeLocations.C,
provider = "abc"
}
},
new class3[]
{
new class3{
ID=32378659,
contractType = BaseContract.ContractTypes.A,
productType = Responsabilite_civile.ProductTypes.A,
startDate = new DateTime(2014,8,2),
finishDate = new DateTime(2015, 1,8),
office_location = BaseContract.OfficeLocations.A,
provider = "abc"
},
new class3{
ID=42395634,
contractType = BaseContract.ContractTypes.A,
productType = Responsabilite_civile.ProductTypes.B,
startDate = new DateTime(2015,7,16),
finishDate = new DateTime(2015, 8,8),
office_location = BaseContract.OfficeLocations.B,
provider = "wer"
},
new class3{
ID=42305635,
contractType = BaseContract.ContractTypes.A,
productType = Responsabilite_civile.ProductTypes.A,
startDate = new DateTime(2015,9,29),
finishDate = new DateTime(2015, 10,6),
office_location = BaseContract.OfficeLocations.B,
provider = "abc"
},
},
new class4[]
{
new class4{
ID=14385634,
contractType = BaseContract.ContractTypes.B,
productType = Protection_persones.ProductTypes.B,
startDate = new DateTime(2015,8,4),
finishDate = new DateTime(2015, 8,9),
office_location = BaseContract.OfficeLocations.A,
provider = "wer"
},
new class4{
ID=14385635,
contractType = BaseContract.ContractTypes.B,
productType = Protection_persones.ProductTypes.B,
startDate = new DateTime(2015,9,11),
finishDate = new DateTime(2015, 9,27),
office_location = BaseContract.OfficeLocations.A,
provider = "abc"
}
}
};
class1[] pContracts = new class1[]{
new class1{
ID=14385634,
contractType = BaseContract.ContractTypes.A,
productType = Protection_persones.ProductTypes.B,
startDate = new DateTime(2015,8,4),
finishDate = new DateTime(2015, 8,9),
office_location = BaseContract.OfficeLocations.A,
provider = "wer"
},
new class1{
ID=14385635,
contractType = BaseContract.ContractTypes.B,
productType = Protection_persones.ProductTypes.A,
startDate = new DateTime(2015,9,11),
finishDate = new DateTime(2015, 9,27),
office_location = BaseContract.OfficeLocations.A,
provider = "abc"
}
};
如你所见,我有一个锯齿状数组,然后是一个简单数组。
当我尝试以IEnumerable的形式获取简单数组数据时,它是有效的。
但当我试图将锯齿数组作为IEnumerable时,它会抛出一个错误
public IEnumerable<class1> GetAllContracts()
{
return pContracts;
}
public IEnumerable<IEnumerable<BaseContract>> getcontracts(){
return contracts;
}
我应该更改锯齿阵列数据结构吗?或者,检索该锯齿阵列的正确wat是什么
谢谢。什么错误?你发布的代码非常有效。真的吗?当我打电话时http://localhost:49269/api/contracts 我得到一个ExceptionType错误。当我打电话的时候http://localhost:49269/api/pContracts 我正确地获取了json,这就是序列化程序的问题。对不起,我帮不了你。C面很好。