Solved

Groupby and add items to a list

Posted on 2008-10-18
4
1,775 Views
Last Modified: 2013-12-17
Hello,

I am writing a C# program and in that I have a List<category> which I have defined. I am doing a group by on the items. My question is, how do I put these elements into the same List or even a different List? Do I need to use IEnumerator? If yes, then how do I do so?

Any help is greatly appreciated!

Thanks
Arjit
0
Comment
Question by:arjitbhandari
  • 2
  • 2
4 Comments
 
LVL 12

Expert Comment

by:jandromeda
ID: 22750201
Your question is little bit unclear to me, but if you are trying to add items to certain places of the current list then you can use either List<T>).Insert(<index>,<T>) method or the List<T>.AddRange(<inderx>,IEnumerable<T>).
0
 
LVL 12

Accepted Solution

by:
jandromeda earned 500 total points
ID: 22750225
Here is an example.
List<string> names = new List<string>();

			

			names.Add("John");

			names.Add("Jimmy");

			names.Add("Jason");

			names.Add("Jack");

			

			foreach (string name in names)

			{

				Console.WriteLine(name);

			}

			

			Console.WriteLine();

			

			// Add a name between Jimmy and Jason

			names.Insert(2, "Jaleem");

			

			foreach (string name in names)

			{

				Console.WriteLine(name);

			}

			

			List<string> moreNames = new List<string>();

			

			moreNames.Add("Mary-Anne");

			moreNames.Add("Samantha");

			

			// Add a collection of names between Jason and Jack

			names.InsertRange(4, moreNames);

			

			Console.WriteLine();

			

			foreach (string name in names)

			{

				Console.WriteLine(name);

			}

Open in new window

0
 

Author Comment

by:arjitbhandari
ID: 22750446
Thanks for your reply.

Basically, I have defined 2 List, ret and ret1, with reference to the following code snippet. When I add elements to ret, I do not have any problem, but when I try to add elements to ret1, which is of same type, I get the following error:

 "Cannot initialize type with a collection initializer because it does not implement 'System.Collections.IEnumerable'"

Note that the purpose of this code is to "group by" elements in List ret. Do you have any suggestions for this?

Thanks
Arjit
ret.Add(new Category()

                            {

                                GivenName = contact.GivenName,

                                //   Surname = contact.Surname,

                                //   PhoneNumber = (string)contact.PhoneNumbers[0].Value,

                                CompanyName = contact.CompanyName,

                                JobTitle = contact.JobTitle,

                                BAddress = contact.PhysicalAddresses[0].Street,

                                City = contact.PhysicalAddresses[0].City,

                                State = contact.PhysicalAddresses[0].State,

                                Zip = contact.PhysicalAddresses[0].PostalCode,

                                Country = contact.PhysicalAddresses[0].CountryOrRegion

                                

                            });

                        var temp = from p in ret

                                   group p by p.Country

                                   ;
 

                        foreach (var i in temp)

                            foreach (var temp2 in i)

                            {

                                ret1.Add(new Category() { temp2.GivenName });

                            }

Open in new window

0
 

Author Comment

by:arjitbhandari
ID: 22750671
I am also attaching the cs file for reference. Thanks in advance.

Arjit
collection.txt
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Sum Column in GridView 3 42
About delegates in c sharp 3 31
Connection String 16 43
C#  Radio button search for Date not DateTime 4 35
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now