Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / October 2007

Tip: Looking for answers? Try searching our database.

Copy Items from a Generic List into multiple Generic Lists

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Sam Shrefler - 12 Oct 2007 16:22 GMT
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
Jon Skeet [C# MVP] - 12 Oct 2007 16:24 GMT
> I have two List<Order> and List<OrderDetails>
>
[quoted text clipped - 20 lines]
>
> 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
Sam Shrefler - 12 Oct 2007 16:41 GMT
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>
>
[quoted text clipped - 34 lines]
>
> - Show quoted text -
Jon Skeet [C# MVP] - 12 Oct 2007 16:51 GMT
> 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
abieganski@gmail.com - 12 Oct 2007 17:04 GMT
> 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
Jon Skeet [C# MVP] - 12 Oct 2007 18:27 GMT
> > Is there an easy way to convert between a List and a Dictionary?
> >
[quoted text clipped - 8 lines]
>                       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.)

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.