Add anonymous object class to anonymous list

I declare an anonymous list, like this one, and it contains a contact list as well

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

I am trying to check the list if ContactGroupKey exists and then update only the contacts (defined as a list), otherwise insert a new contact group. but when I tried to add a new contact group inside my anonymous list, it will throw the error "Best overloaded method matching for" System.Collections.Generic.List.Add (AnonymousType # 2) "has some invalid arguments" I am using an anonymous list for the first time . I tried to avoid classes in this scenario. Can someone suggest me where I made a 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; } 
+2
list generics c # anonymous-types
source share
2 answers

The problem is that the anonymous types you are using do not match. You need the properties to match the 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) }); 

This will be ContractGroupKey and ContactGruopTLK as long properties, and Contacts as List<T> , where T is your other anonymous type. You will 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(); 

This may work - but it will be better for you to create named classes for this. Ultimately, it looks like they are likely to be significant entities in your system, so it’s worth the effort to model them as named types for starters.

(Side note: it should be consistent in your name, usually camelCased local variables, so you should use contactGroup , not contactGroup , etc.)

+2
source share

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

0
source share

All Articles