1

私はクラスワンを持っています

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

クラス 1 のリストに含まれるデータ

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

このリスト 1 をリスト 2 に変換したい

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 に答える