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

本文关键字:列表 对象 类型转换 类型 另一种 一种 | 更新日期: 2023-09-27 18:04:00

我正在尝试将一个对象的特定类型的列表转换为另一个。

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

感谢大家的回答。

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

你可以这样做:

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();
}

试试这个:

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();

…这里更明确一点:

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