将数据从一个类更改为另一个类?

时间:2013-07-29 08:26:31

标签: c# .net

我有第一课

Class One
{
 public string Name{get;set;}
 public string Address {get;set;}
 public string PhoneNo {get;set;}
 public bool IsPrimary{get;set;}
}

第一类包含数据列表

Name    Address PhoneNo IsPrimary
Manoj   ktm     980123   1
Manoj   ktm     980124   0
Manoj   brt     980133   1
Manoj   brt     980134   0
Saroj   pkh     980121   1

我想将此List One转换为List Two

Class Two
{
 public string Name{get;set;}
 public List<Three> Info{get;set;}
}
Class Three
{
 public string Address{get;set;}
 public List<Four> Detail{get;set;}
}
Class Four
{
 public string PhoneNo {get;set;}
 public bool IsPrimary{get;set;}
}

2 个答案:

答案 0 :(得分:1)

请查看我的代码:

void Main()
{
List<One> l = new List<One>();
l.Add(new One {Name = "Manoj", Address = "ktm", PhoneNo = "980123", IsPrimary = true});
l.Add(new One {Name = "Manoj", Address = "ktm", PhoneNo = "980124", IsPrimary = false});
l.Add(new One {Name = "Manoj", Address = "brt", PhoneNo = "980133", IsPrimary = true});
l.Add(new One {Name = "Manoj", Address = "brt", PhoneNo = "980134", IsPrimary = false});
l.Add(new One {Name = "Saroj", Address = "pkh", PhoneNo = "980121", IsPrimary = true});

List<Two> t;
t = l.GroupBy 
 (
    x => x.Name
 )
 .Select 
 (
    x => 
    new Two 
    {
    Name = x.Key, 
    Info = l.Where(z => z.Name == x.Key)
            .GroupBy (
            z => new 
            {
                z.Name,
                z.Address
            })
            .Select 
            (
                z => new Three 
                {
                    Address = z.Key.Address, 
                    Detail = l.Where
                    (
                        d => d.Name == z.Key.Name && d.Address == z.Key.Address
                        )
                        .GroupBy 
                        (
                            d => new
                            {
                                d.Name,
                                d.Address,
                                d.PhoneNo
                            }
                        )
                        .Select 
                        (
                            d => new Four 
                            {
                                PhoneNo = d.Key.PhoneNo, 
                                IsPrimary = d.SingleOrDefault().IsPrimary
                            }
                        ).ToList()
                    }
                ).ToList()
    }
).ToList();
}
class One
{
 public string Name { get; set; }
 public string Address {get;set;}
 public string PhoneNo {get;set;}
 public bool IsPrimary {get;set;}
}
class Two
{
 public string Name {get;set;}
 public List<Three> Info {get;set;}
}
class Three
{
 public string Address {get;set;}
 public List<Four> Detail {get;set;}
}
class Four
{
 public string PhoneNo {get;set;}
 public bool IsPrimary {get;set;}
}

答案 1 :(得分:1)

你可以这样做: 您的问题中的变量约定按原样使用。

List<classone> infos= your data;
List<ClassTwo> lstTwo=new List<Two>();
foreach(var i in infos)
{
ClassFour objfour=new ClassFour();
objFour.PhoneNo=i.PhoneNo;
objFour.IsPrimary=i.IsPrimary;

List<ClassFour> lstFour=new List<ClassFour>();
lstFour.Add(objFour);

ClassThree three=new ClassThree(){
Detail=lstFour,
Address=i.Address
}
List<ClassThree> lstThree=new List<ClassThree>();

ClassTwo two=new ClassTwo{
info=lstTwo,
Name=i.Name
}

lstTwo.Add(two);
}