1

我有第一类

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

我想将此列表一转换为列表二

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

2 回答 2

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;}
}
于 2013-07-29T08:49:50.717 回答
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);
}
于 2013-07-29T10:41:52.840 回答