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

C# 将数据从一个类更改为另一个类?,c#,.net,C#,.net,我有一班的学生 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

我有一班的学生

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
我想把这个列表一转换成列表二

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;}
}
第二类
{
公共字符串名称{get;set;}
公共列表信息{get;set;}
}
三班
{
公共字符串地址{get;set;}
公共列表详细信息{get;set;}
}
四班
{
公共字符串PhoneNo{get;set;}
公共bool IsPrimary{get;set;}
}

请查看我的代码:

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;}
}
void Main()
{
列表l=新列表();
l、 添加(新的{Name=“Manoj”,Address=“ktm”,PhoneNo=“980123”,IsPrimary=true});
l、 添加(新的{Name=“Manoj”,Address=“ktm”,PhoneNo=“980124”,IsPrimary=false});
l、 添加(新的{Name=“Manoj”,Address=“brt”,PhoneNo=“980133”,IsPrimary=true});
l、 添加(新的{Name=“Manoj”,Address=“brt”,PhoneNo=“980134”,IsPrimary=false});
l、 添加(新的{Name=“Saroj”,Address=“pkh”,PhoneNo=“98012”,IsPrimary=true});
列表t;
t=l.GroupBy
(
x=>x.Name
)
.选择
(
x=>
新二
{
Name=x.Key,
Info=l.Where(z=>z.Name==x.Key)
.群比(
z=>新的
{
z、 名字,
z、 地址
})
.选择
(
z=>新三
{
地址=z.Key.Address,
细节=l.其中
(
d=>d.Name==z.Key.Name&&d.Address==z.Key.Address
)
.群比
(
d=>新的
{
d、 名字,
d、 地址:,
d、 菲诺
}
)
.选择
(
d=>新的四个
{
PhoneNo=d.Key.PhoneNo,
IsPrimary=d.SingleOrDefault().IsPrimary
}
)托利斯先生()
}
)托利斯先生()
}
).ToList();
}
一班
{
公共字符串名称{get;set;}
公共字符串地址{get;set;}
公共字符串PhoneNo{get;set;}
公共bool IsPrimary{get;set;}
}
二班
{
公共字符串名称{get;set;}
公共列表信息{get;set;}
}
三班
{
公共字符串地址{get;set;}
公共列表详细信息{get;set;}
}
四班
{
公共字符串PhoneNo{get;set;}
公共bool IsPrimary{get;set;}
}

您可以这样实现: 问题中变量的约定按原样使用

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);
}
List infos=您的数据;
List lstwo=新列表();
foreach(infos中的var i)
{
ClassFour objfour=新的ClassFour();
objFour.PhoneNo=i.PhoneNo;
objFour.IsPrimary=i.IsPrimary;
List lstFour=新列表();
添加(objFour);
ClassThree=新的ClassThree(){
细节=LST4,
地址
}
List lstThree=新列表();
ClassTwo=新ClassTwo{
info=lstwo,
Name=i.Name
}
二.增加(二);
}

开关一是主键。在你们班one@VeeKayBee不,因为他不只是想转换对象,他想将列表转换成某种分组列表我不知道我的问题与之匹配,我能在foreach循环中转换这个吗?林克·奎里很慢,这是可行的,但你认为会更快吗?我没有做任何基准测试。。。