重构条件语句和 foreach 循环。

huangapple go评论74阅读模式
英文:

Refactoring ifs and a foreach

问题

我在一个按照用户角色对用户进行排序的函数中有两个if条件语句。每个用户是一个具有属性如名称、密码等的对象,这些属性只是简单的字符串,但每个用户都有一个名为“Role”的内部对象,该对象具有一个名为“Name”的属性,该属性是一个字符串。代码看起来像这样:

public GetUsersSorted(string sortBy)
{
    if(sortBy=='roles')
    {
        usersCollection = 获取用户,但不按任何顺序排序
    }
    else
    {  
        usersCollection = 获取按其他方式排序的用户
    }
    
    items = 将用户映射到DTO(usersCollection)
    
    if(sortBy=='roles')
    {
        items = 在内存中对用户进行排序(items)
    }
}

private mapUsersToDTO(List usersSorted)
{
    foreach(var item in usersSorted)
    {
        item.Roles = item.Roles.OrderBy(i => i.Name).ToList(); //在这里对用户的角色进行排序
    }
    
    usersSorted = usersSorted.OrderBy(b => b.Roles.First().Name()).ToList();
    
    return usersSorted;
}

如何基于角色的名称更高效地对它们进行排序?这种方式也可以,但是我想基于所有角色的名称对它们进行排序。

英文:

I have two ifs in a function that sorts users by their roles, the users that access an application. Each user is an object with properties like name, password, which are just simple strings, but each user has an inner object, named Role that has a property Name, which is a string. The code looks something like:

    public GetUsersSorted(string sortBy)
       {
             if(sortBy=='roles')
             {
                 usersCollection = get the users, but not sorted by anything
             }
             else
             {  
                 usersCollection = get users sorted sorted by something else
             }
        
             items = mapUsersToDTO(usersCollection)
        
             if(sortBy=='roles')
             {
                 items = sortUsersInMemory(items)
             }
       }

private mapUsersToDTO(List usersSorted)
{
    foreach(var item in usersSorted)
    {
      item.Roles = item.Roles.OrderBy(i => i.Name).ToList(); //here I sort the roles inside the user
    }

    usersSorted = usersSorted.OrderBy(b => b.Roles.First().Name()).ToList();

    return usersSorted;
}

How can I sort them more efficiently, based on the Name of their Role? This works too but, I sort them based only on the first role, and I want to sort them based on all the names of their roles

答案1

得分: 1

以下是翻译好的内容:

听起来你的列表中有一个嵌套的列表,你想要嵌套的列表也是有序的。

users.Select(x => x.Roles.OrderBy(y => y.Name).ToList())
.OrderBy(x => x.Name).ToList();

这会对内部列表进行排序,然后对外部列表进行排序。

如果你发布一些实际的代码或者可能是你数据的模型,我可以更好地调整这个解决方案。

英文:

Sounds like you've got a list in your list and you want the nested list also ordered.

users.Select(x=>x.Roles.OrderBy(y=>y.Name).ToList())
     .OrderBy(x=>x.Name).ToList();

This orders the internal list, then orders the outer list.

If you posted some actual code or maybe the model for your data, I could tailor that solution better.

huangapple
  • 本文由 发表于 2023年5月25日 01:46:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76326187.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定