在C#中用List <>替换ArrayList

dcp*_*ers 1 c#

我正在广泛使用ArrayList并且难以使用此List <>.我使用EntitySpace ORM来做DAL的事情.这个东西很好用,但问题是我必须用对象类型定义List <>,抱怨它不能转换它.

非常感谢你的帮助.

原始使用ArrayList:

public ArrayList Get()
    {
        TndCustomerTendersCollection collection = new TndCustomerTendersCollection();
        collection.Query
        .Select
        (
            collection.Query.CustomerTenderID,
            collection.Query.CustomerTenderID,
            collection.Query.CustomerTenderCode,
            collection.Query.CustomerTenderName,
            collection.Query.StartDate,
            collection.Query.DueDate,
            collection.Query.CompleteDate,
            collection.Query.DateCreated,
            collection.Query.LastDateModified
        )
        .Where
        (
            collection.Query.IsActive.Equal(true)
        );

    ArrayList list = new ArrayList ();
        foreach (TndCustomerTenders item in collection)
        {
            list.Add(item);
        }
        return list;
    }
Run Code Online (Sandbox Code Playgroud)

用List替换后

public List<Tender> Get()
    {
        TndCustomerTendersCollection collection = new TndCustomerTendersCollection();
        collection.Query
        .Select
        (
            collection.Query.CustomerTenderID,
            collection.Query.CustomerTenderID,
            collection.Query.CustomerTenderCode,
            collection.Query.CustomerTenderName,
            collection.Query.StartDate,
            collection.Query.DueDate,
            collection.Query.CompleteDate,
            collection.Query.DateCreated,
            collection.Query.LastDateModified
        )
        .Where
        (
            collection.Query.IsActive.Equal(true)
        );

        // HOW DO CONVERT THAT TO THAT LIST

        List<Tender> list = new List<Tender>();
        foreach (TndCustomerTenders item in collection)
        {
            list.Add(item);
        }
        return list;
    }
Run Code Online (Sandbox Code Playgroud)

Ala*_*lan 6

TndCustomerTenders和Tender是两种不同的类型.

您需要显式从TndCustomerTenders转换为Tender,或者您需要定义隐式转换.

List<Tender> list = new List<Tender>();
        foreach (TndCustomerTenders item in collection)
        {
            //assumes conversion via constructor
            list.Add(new Tender(item));
        }
Run Code Online (Sandbox Code Playgroud)

要么

List<Tender> list = new List<Tender>();
        foreach (TndCustomerTenders item in collection)
        {
            Tender t = new Tender() { foo = item.foo, bar = item.bar };
            list.Add(t);
        }
Run Code Online (Sandbox Code Playgroud)