PC Review


Reply
Thread Tools Rate Thread

Copy Items from a Generic List into multiple Generic Lists

 
 
Sam Shrefler
Guest
Posts: n/a
 
      12th Oct 2007
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

 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      12th Oct 2007
On Oct 12, 4:22 pm, Sam Shrefler <sshref...@gmail.com> wrote:
> 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

 
Reply With Quote
 
Sam Shrefler
Guest
Posts: n/a
 
      12th Oct 2007
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


On Oct 12, 11:24 am, "Jon Skeet [C# MVP]" <sk...@pobox.com> wrote:
> On Oct 12, 4:22 pm, Sam Shrefler <sshref...@gmail.com> wrote:
>
>
>
>
>
> > 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 -



 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      12th Oct 2007
On Oct 12, 4:41 pm, Sam Shrefler <sshref...@gmail.com> wrote:
> 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

 
Reply With Quote
 
abieganski@gmail.com
Guest
Posts: n/a
 
      12th Oct 2007
> 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

 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      12th Oct 2007
<(E-Mail Removed)> wrote:
> > 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);
> });


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.)

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to convert a generic list to Outlook.Items list? jeremybo Microsoft Outlook Program Addins 0 2nd Apr 2009 02:29 PM
Generic List - Copy Doug Microsoft Dot NET Framework 2 20th Jul 2007 08:36 PM
how to view generic list items in watch window ib Microsoft VC .NET 1 5th Dec 2006 05:02 PM
error C2955: 'Vector' : use of class generic requires generic argument list Herby Microsoft VC .NET 5 14th Mar 2006 03:55 PM
List<t> generic type - Fastest way to copy into an unmanaged array? Daniel Mori Microsoft VC .NET 2 12th Oct 2004 01:58 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:23 AM.