2016-04-29 6 views
0

あるコレクションから別のコレクションにデータをコピーしたい。私は次のコードを使用しています。あるコレクションから別のコレクションにデータをコピーするC#

foreach (var x in invVM.invFreeItemsWithBatch) 
      { 
       invVM.invitemsWithBatch.Add(x); 
      } 

これは間違っています。どうすれば修正できますか?

enter image description here

public ObservableCollection<InvFreeItemWithBatch> invFreeItemsWithBatch = new ObservableCollection<InvFreeItemWithBatch>(); 
    public ObservableCollection<InvFreeItemWithBatch> AddInvFreeItemsByBatch(int intitemID, string stritem, double dblqty, double dblamount, string struOM, bool blnfreeIssue, 
     double dbluPrice, double dbldiscPerce, double dbldiscAmount, string strbatch, double dblBalanceForFreeqty) 
    { 
     invFreeItemsWithBatch.Add(new InvFreeItemWithBatch(intitemID, stritem, dblqty, dblamount, struOM, blnfreeIssue, dbluPrice, dbldiscPerce, 
      dbldiscAmount, strbatch, dblBalanceForFreeqty)); 
     stritem = ""; struOM = ""; 
     return invFreeItemsWithBatch; 
    } 

    public ObservableCollection<InvItemWithBatch> invitemsWithBatch = new ObservableCollection<InvItemWithBatch>(); 
    public ObservableCollection<InvItemWithBatch> AddInvItemsByBatch(int intitemID, string stritem, double dblqty, double dblamount, string struOM, bool blnfreeIssue, 
     double dbluPrice, double dbldiscPerce, double dbldiscAmount, string strbatch, double dblBalanceForFreeqty) 
    { 
     invitemsWithBatch.Add(new InvItemWithBatch(intitemID, stritem, dblqty, dblamount, struOM, blnfreeIssue, dbluPrice, dbldiscPerce, 
      dbldiscAmount, strbatch, dblBalanceForFreeqty)); 
     stritem = ""; struOM = ""; 
     return invitemsWithBatch; 
    } 
+1

invitemsWithBatchとinvFreeItemsWithBatchは何ですか? – BugFinder

+0

AShは言ったように、何が「間違っている」ものなのかを知る必要があります。おそらく、あなたはこの質問/答えを見ることもできます:http://stackoverflow.com/questions/4493858/elegant-way-to-combine-multiple-collections-of-elements –

+1

[私はどうすればいいですか? foreachせずにリストからリストに項目をコピーする?](http://stackoverflow.com/questions/1952185/how-do-i-copy-items-from-list-to-list-without-foreach) –

答えて

0

enter image description here invFreeItemsWithBatchinvitemsWithBatch以外の元素の異なるタイプを含みます。

リストには、InvItemWithBatch要素のタイプが含まれ、もう1つにはInvFreeItemWithBatchが含まれます。

好きな場合は、foreachに新しい要素を作成できます。

class InvFreeItemWithBatch : InvItemWithBatch 

あなたは

foreach (var x in invVM.invFreeItemsWithBatch) 
     { 
      invVM.invitemsWithBatch.Add(x); 
     } 

を入力することができますしかし、あなたが考慮しなければならない、2つのリストそのような方法:

foreach (var x in invVM.invFreeItemsWithBatch) 
{ 
    var newItem = new InvFreeItemWithBatch(); 
    // set the values of newItem to match the values of x 
    // ... 
    invVM.invitemsWithBatch.Add(newItem); 
} 

またInvItemWithBatchInvFreeItemWithBatchの基本クラスを行うことを検討xの同じ参照があります。

関連する問題