2

I've declare a anonymous list like this, and it contain list of contact also

var ContactGroup = new[] { new { ContactGroupKey = 0, ContactGroupTLK = 0, Desc = "",  Contacts=new List<object>() } }.ToList();

I try to check the list, if the ContactGroupKey is exists then update only the Contacts(defined as a list) else insert a new contactgroup. but when i tried to add a new contactgroup inside my anonymous list its throw an error "The best overloaded method match for 'System.Collections.Generic.List.Add(AnonymousType#2)' has some invalid arguments " I'm using anonymous list first time. I tried to avoid classes in this scenario. can any one suggest me where i made the mistake?

 while()
 {
    var Contact= new {
                       ContactKey = Convert.ToInt64(DB["ContactKey", "0"]),                       
                       FirstName = DB["FirstName", ""].ToString(),
                       Surname = DB["Surname", ""].ToString(),
                       FullName = DB["Fullname", ""].ToString(),
                       Street = DB["bStreet", ""].ToString(),
                       City = DB["bCity", ""].ToString(),
                     };
     foreach (var item in ContactGroup)
                {
                    if (item.ContactGroupKey == Contact.ClaimContactGroupKey)
                    {
                        item.Contacts.Add(Contact);
                        added = true;
                    }
                }
     if(!added){

      ContactGroup.Add(new {
                           ContactGroupKey = Convert.ToInt64(DB["ContactGroupKey", "0"]),
                           ContactGroupTLK = Convert.ToInt64(DB["TranslationKey", "0"]),
                           Desc = DB["Description", ""].ToString(),
                           Contacts=GenerateList(Contact)
                           });          
       }
 }// End While


public static List<T> GenerateList<T>(T itemOftype)
 {
     List<T> newList = new List<T>();
     return newList;
 } 
Arooran
  • 637
  • 1
  • 17
  • 31

2 Answers2

3

The problem is that the anonymous types you're using aren't the same. You need to get the properties to match in name, type and order. Look at this:

  ContactGroup.Add(new {
                       ContactGroupKey = Convert.ToInt64(DB["ContactGroupKey", "0"]),
                       ContactGroupTLK = Convert.ToInt64(DB["TranslationKey", "0"]),
                       Desc = DB["Description", ""].ToString(),
                       Contacts=GenerateList(Contact)
                       });

That will have ContractGroupKey and ContactGruopTLK as long properties, and Contacts as a List<T> where T is your other anonymous type. You would need to change your initialization to be something like:

var sampleContact = new { ContactKey = 0L, /* etc */ };
var sampleContactList = new[] { sampleContact }.ToList();
var contactGroup = new[] { new { ContactGroupKey = 0L,
                                 ContactGroupTLK = 0L, 
                                 Desc = "", 
                                 Contacts = sampleContactList } }.ToList();

That could work - but you'd be better off creating named classes for this. Ultimately it feels like these are likely to be significant entities in your system, so it's worth putting the effort into modelling them as named types to start with.

(Side note: it's worth being consistent in your naming; normally local variables are camelCased, so you should use contactGroup rather than ContactGroup, etc.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thank you for your reply.I'm a big fan of your book.I can understand creating a class is more worth than doing with anonymous types.I do concern about the variable naming. I'm re writing the above code with classes. – Arooran Jan 28 '13 at 14:37
0

Anonymous types are not intended to leave the current method. You cannot pass them to other methods. You should define a class containing the properties you need to handle.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Although it is true that writing a class will probably be the best course of action here, you can actually pass anonymous type variables to methods if you use generics or `dynamic` arguments. – Gorpik Jan 28 '13 at 14:43