添加一个匿名类对象的一个​​匿名列表 [英] Add an anonymous class of object to an anonymous list

查看:120
本文介绍了添加一个匿名类对象的一个​​匿名列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经声明了一个匿名列表这样的,它包含联系人的名单也

  VAR ContactGroup =新的[] {新{ContactGroupKey = 0,ContactGroupTLK = 0,说明=,联系人=新的名单,其中,对象>()}} .ToList();
 

我尝试检查清​​单,如果ContactGroupKey是存在,那么只更新联系人(定义为列表)否则插入新的contactgroup。但是当我试图添加一个新的contactgroup在我匿名列表及其引发错误的最佳重载方法匹配'System.Collections.Generic.List.Add(AnonymousType#2)有一些无效参数  我使用匿名列表的第一次。我试图避免这种情况的类。可以任何一个建议我在哪里,我犯的错?

 ,而()
 {
    VAR联系=新{
                       ContactKey = Convert.ToInt64(DB [ContactKey,0]),
                       名字= DB [名字,]的ToString()
                       姓= DB [姓氏,]的ToString()
                       全名= DB [全名,]的ToString()
                       街道= DB [bStreet,]的ToString()
                       市= DB [bCity,]的ToString()
                     };
     的foreach(在ContactGroup VAR项)
                {
                    如果(item.ContactGroupKey == Contact.ClaimContactGroupKey)
                    {
                        item.Contacts.Add(联系);
                        添加= TRUE;
                    }
                }
     如果(!加){

      ContactGroup.Add(新{
                           ContactGroupKey = Convert.ToInt64(DB [ContactGroupKey,0]),
                           ContactGroupTLK = Convert.ToInt64(DB [TranslationKey,0]),
                           说明= DB [说明,]的ToString()
                           联系方式= GenerateList(联系)
                           });
       }
 } //端,而


公共静态列表< T> GenerateList< T>(T itemOftype)
 {
     名单< T> newList =新的名单,其中,T>();
     返回newList;
 }
 

解决方案

问题是,你正在使用的匿名类型不一样。你需要得到的属性,以名称,类型和顺序相匹配。看看这个:

  ContactGroup.Add(新{
                       ContactGroupKey = Convert.ToInt64(DB [ContactGroupKey,0]),
                       ContactGroupTLK = Convert.ToInt64(DB [TranslationKey,0]),
                       说明= DB [说明,]的ToString()
                       联系方式= GenerateList(联系)
                       });
 

这将有 ContractGroupKey ContactGruopTLK 属性和联系人名单,其中,T> ,其中 T 是其他匿名类型。你可能需要改变你的初始化是这样的:

  VAR sampleContact =新{ContactKey = 0L,/ *等* /};
VAR sampleContactList =新的[] {} sampleContact .ToList();
VAR contactGroup =新的[] {新{ContactGroupKey = 0L,
                                 ContactGroupTLK = 0L,
                                 说明=,
                                 联系方式= sampleContactList}} .ToList();
 

这的可以的工作 - 但你会更好创造这个命名类。最终感觉这很可能是在您的系统显著的实体,所以它是值得投入的精力投入到他们的模型命名的类型开始。

(附注:这是值得被你的命名保持一致;通常局部变量驼峰,所以你应该使用 contactGroup ,而不是 ContactGroup ,等等。)

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;
 } 

解决方案

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

这篇关于添加一个匿名类对象的一个​​匿名列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆