2009-09-09 46 views
1

我广泛使用ArrayList并且难以使用此列表<>。我正在使用EntitySpace ORM做DAL的东西。这个东西很好用,但问题是,我不得不定义列表<>与抱怨它不能转换的对象类型。用C#中的List <>替换ArrayList

我很感谢您的帮助。

原使用的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; 
    } 

与清单更换

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; 
    } 
+1

TndCustomerTenders延伸投标吗?因为在第二个例子中,你有一个Tender对象列表,而不是TndCustomerTenders对象。要转换上面的内容,可以使用List

+1

列表不是List ? –

+1

您的foreach循环不应该是:foreach(收集中的投标项)? –

回答

6

TndCustomerTenders和招标两种不同的类型。

您需要将TndCustomerTenders显式转换为Tender,否则您需要定义隐式转换。

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

List<Tender> list = new List<Tender>(); 
     foreach (TndCustomerTenders item in collection) 
     { 
      Tender t = new Tender() { foo = item.foo, bar = item.bar }; 
      list.Add(t); 
     } 
+0

我喜欢这个想法...非常感谢。 – dcpartners

2

更改后

public List<Tender> Get() 

public List<TndCustomerTenders> Get() 

和更改

List<Tender> list = new List<Tender>(); 

List<TndCustomerTenders> list = new List<TndCustomerTenders>(); 

或者,如果你使用的是最新的框架,

var list = new List<TndCustomerTenders>(); 
+0

我想输出为Tender,因为这是我在mt BUsiness对象中定义的,而TndCustomerTenders是来自DAL的对象。 – dcpartners

2
return collection.ToList<Tender>(); 
+0

很酷。不知道ToList有一个通用的方法来做到这一点。 – spender

+0

这假定TndCustomerTendersCollection提供了ToList()(它可能不)和两个,Tender和TndCustomerTenders之间存在转换 – Alan

+0

该集合提供NO ToList()方法不幸。 – dcpartners

1

这是你想要的吗?

.Where(collection.Query.IsActive.Equal(true)).Cast<Tender>().ToList() 
+0

不幸的是,我所拥有的集合并没有上述方法。 – dcpartners

相关问题