将锯齿状的对象数组返回为IEnumerable <ienumerable <object>&gt;

时间:2015-10-29 12:18:19

标签: c# asp.net-web-api ienumerable

我正在使用ASP.NET API,并且我有一些issuses从基类检索不同派生对象的锯齿状数组。 所以我有这个基类:

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<class1>时,它可以工作。 但是当我尝试将锯齿状数组作为IEnumerable<IEnumerable<BaseContract>>时,它会抛出一个错误。

    public IEnumerable<class1> GetAllContracts()
    {
        return pContracts;
    }

    public IEnumerable<IEnumerable<BaseContract>> getcontracts(){
        return contracts;
    }

我应该更改锯齿状阵列数据结构吗?或者检索锯齿状阵列的正确值是什么?

谢谢。

0 个答案:

没有答案