如何将一种类型的列表与对象转换为另一种?

时间:2014-05-19 17:31:10

标签: c# list

我试图将特定类型对象的列表转换为另一个对象。这是原始类型;

 public class OriginialType
 {
    public string Module { get; set; }
    public string Screen { get; set; }
    public string Permission { get; set; }
 } 

以下是此目标类型的列表;

List<OriginialType> orignialList = new List<OriginialType>();
orignialList.Add(new OriginialType { Module = "Module1", Screen = "Dashboard", Permission = "Add" });
orignialList.Add(new OriginialType { Module = "Module1", Screen = "Dashboard", Permission = "Edit" });
orignialList.Add(new OriginialType { Module = "Module1", Screen = "Dashboard", Permission = "Delete" });

这是我想要列表的目标类型;

public class TargetType
{
    public string Module { get; set; }
    public List<Screen> Screen { get; set; }
}

public class Screen
{
    public string ScreenName { get; set; }
    public string Permission { get; set; }
}

因此,我没有一个包含重复包含相同模块的项目的列表,而是需要一个存储模块一次的列表,同时让所有屏幕都与之关联。如何将此OriginalType转换为我的TargetType?

编辑:@ itsme86的解决方案完美无缺。但我意识到我需要屏幕的名称才能显示出来。所以修改了他的解决方案。首先将我的TargetType更改为;

public class TargetType
{
    public string Module { get; set; }
    public List<Screen> Screen { get; set; }
}

public class Screen
{
    public string ScreenName { get; set; }
    public List<Permission> Permission { get; set; }
}

public class Permission
{
    public string PermissionName { get; set; }
}

整个转换方法为;

 foreach (var grouping in roleRightsList.GroupBy(o => new { o.Module, o.Screen }))
        {
            TargetModel target = new TargetModel { Module = grouping.Key.Module };
            targetList.Add(target);

            Screen targetScreen = new Screen { ScreenName = grouping.Key.Screen };
            targetScreen.Permission = grouping.Select(o => new Permission
            {
                PermissionName = o.Permission,
            }).ToList();

            target.Screen = new List<Screen>();
            target.Screen.Add(targetScreen);
        }

感谢所有答案。

3 个答案:

答案 0 :(得分:0)

你可以做这样简单的事情:

List<TargetType> targetList = new List<TargetList>();

foreach (var grouping in originalList.GroupBy(o => o.Module))
{
    TargetType target = new TargetType { Module = grouping.Key };
    targetList.Add(target);
    target.Screen = grouping.Select(o => new Screen
       {
          ScreenName = o.Screen,
          Permission = o.Permission
       }).ToList();
}

答案 1 :(得分:0)

试试这个:

var targetTypes = orignialList.GroupBy(o => o.Module)
    .Select(
        g =>
            new TargetType
            {
                Module = g.Key,
                Screen =
                    g.Select(t => new Screen {Permission = t.Permission, ScreenName = t.Screen}).ToList()
            }).ToList();

答案 2 :(得分:0)

..在这里更明确一点:

List<TargetType> targetTypeList = new List<TargetType>();
List<IGrouping<string, OriginialType>> grouping = orignialList.GroupBy(o => o.Module).ToList();

foreach(IGrouping<string, OriginialType> group in grouping)
{
    TargetType newItem = new TargetType() { Module = group.Key, Screen = new List<Screen>() };                

    foreach(OriginialType o in group)
        newItem.Screen.Add(new Screen() { ScreenName = o.Screen, Permission = o.Permission });   

    targetTypeList.Add(newItem);
}