0

I want to copy data from one collection to another. I'm using following code.

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

But this is wrong. How can I correct?

enter image description here

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;
    }
Tom
  • 1,343
  • 1
  • 18
  • 37
  • 1
    what are invitemsWithBatch and invFreeItemsWithBatch ? – BugFinder Apr 29 '16 at 06:54
  • As ASh said, we need to know what is "wrong" what is the error? Maybe in the mean time you can look also at this question/answer: http://stackoverflow.com/questions/4493858/elegant-way-to-combine-multiple-collections-of-elements – jason.kaisersmith Apr 29 '16 at 06:55
  • 1
    Possible duplicate of [How do I copy items from list to list without foreach?](http://stackoverflow.com/questions/1952185/how-do-i-copy-items-from-list-to-list-without-foreach) – Iwo Kucharski Apr 29 '16 at 06:55

1 Answers1

0

invFreeItemsWithBatch contains different type of elements than invitemsWithBatch.

One list contains the type of InvItemWithBatch elements, the other contains InvFreeItemWithBatch.

You could create new element in the foreach if You like.

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);
}

Also consider making InvItemWithBatch the base class of InvFreeItemWithBatch:

class InvFreeItemWithBatch : InvItemWithBatch

that way You can type

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

But You have to consider, that the two list will have the same reference for the x.

ntohl
  • 2,067
  • 1
  • 28
  • 32