Copy Items from a Generic List into multiple Generic Lists

  • Thread starter Thread starter Sam Shrefler
  • Start date Start date
S

Sam Shrefler

I have two List<Order> and List<OrderDetails>

Order looks like this:
{
int orderID
List<OrderDetails> details
orderTotal
orderDate
...
}

I have a List that contains ALL OrderDetails and looks like this:
{
int orderID
Details details
...
}

What I'm looking to do process the second list so that i copy each
corresponding OrderDetail into the parent Order details.

What would be the best way to go about this?

Thanks

Sam
 
I have two List<Order> and List<OrderDetails>

Order looks like this:
{
int orderID
List<OrderDetails> details
orderTotal
orderDate
...

}

I have a List that contains ALL OrderDetails and looks like this:
{
int orderID
Details details
...

}

What I'm looking to do process the second list so that i copy each
corresponding OrderDetail into the parent Order details.

What would be the best way to go about this?

I'd create a Dictionary<int,Order> so that you can get from the order
ID to the order quickly. Then just do something like:

foreach (OrderDetails details in listOfOrderDetails)
{
Order order = orders[details.orderID];
order.details.Add(details);
}

Jon
 
Jon:

Thanks.

Is there an easy way to convert between a List and a Dictionary?

My List is already populated through an automatic DataReader->List
generator and given to me as a List...

Thanks

Sam


I have two List<Order> and List<OrderDetails>
Order looks like this:
{
int orderID
List<OrderDetails> details
orderTotal
orderDate
...

I have a List that contains ALL OrderDetails and looks like this:
{
int orderID
Details details
...

What I'm looking to do process the second list so that i copy each
corresponding OrderDetail into the parent Order details.
What would be the best way to go about this?

I'd create a Dictionary<int,Order> so that you can get from the order
ID to the order quickly. Then just do something like:

foreach (OrderDetails details in listOfOrderDetails)
{
Order order = orders[details.orderID];
order.details.Add(details);

}

Jon- Hide quoted text -

- Show quoted text -
 
Is there an easy way to convert between a List and a Dictionary?

foreach (Order order in orders)
{
orderDictionary[order.orderID] = order;
}

is probably about as easy as you can get. (It's slightly easier with
LINQ in C# 3, admittedly...)

Jon
 
Is there an easy way to convert between a List and a Dictionary?
My List is already populated through an automatic DataReader->List
generator and given to me as a List...


This should work pretty well:

Dictionary<int, Order> orders = new Dictionary<int, Order>();
list.ForEach(delegate(Order o)
{
orders.Add(o.orderID, o);
});


Cheers,
Adam
___________________
ab
http://godevelop.blogspot.com
 
This should work pretty well:

Dictionary<int, Order> orders = new Dictionary<int, Order>();
list.ForEach(delegate(Order o)
{
orders.Add(o.orderID, o);
});

That will certainly work - but I think for situations as simple as
that, I'd usually use the normal foreach loop instead of the ForEach
method taking a delegate. Aside from anything else, introducing
captured variables can make life complicated in a hurry, and the
debugging is more "interesting" with the delegate version.

(In other cases where it brings a benefit, of course, it's a different
matter.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top