重构这段代码

发布于 2024-09-26 06:24:57 字数 1285 浏览 2 评论 0原文

我正在尝试根据来自各个部门的工作人员列表(lstStaff)填充树视图。

public class Staff
{
    public int StaffID { get; set; }
    public string StaffName { get; set; }
    public int DeptID { get; set; }
    public string DepartmentName { get; set; }
}

结果应该是这样的:

Department A

  • John
  • Dave

Department B

  • Andy
  • Leon

我编写了以下代码。不过,我认为代码应该进一步重构。

RadTreeNode deptNode;
RadTreeNode staffNode;
var departments = (from d in lstStaff
                   where !string.IsNullOrEmpty(d.DepartmentName) 
                select new { d.DeptId, d.DepartmentName })
                .Distinct();

foreach (var d in departments)
{
    //add department node
    deptNode = new RadTreeNode()
    {
        Value = d.DeptId,
        Text = d.DepartmentName
    };
    tvwDepartmentCases.Nodes.Add(deptNode);

    //add staff nodes to department node
    var staffs = lstStaff
                    .ToList()
                    .Where(x => x.DeptId == d.DeptId);

    foreach (var s in staffs)
    {
        staffNode = new RadTreeNode()
        {
            Value = s.StaffID,
            Text = s.StaffName
        };
        deptNode.Nodes.Add(staffNode);
    }
}

欢迎任何评论。谢谢!

I am trying to populate a tree view based on a list of staff from various deparments (lstStaff).

public class Staff
{
    public int StaffID { get; set; }
    public string StaffName { get; set; }
    public int DeptID { get; set; }
    public string DepartmentName { get; set; }
}

The result should be like this:

Department A

  • John
  • Dave

Department B

  • Andy
  • Leon

I have written the following code. However, I think the code should be refactored further.

RadTreeNode deptNode;
RadTreeNode staffNode;
var departments = (from d in lstStaff
                   where !string.IsNullOrEmpty(d.DepartmentName) 
                select new { d.DeptId, d.DepartmentName })
                .Distinct();

foreach (var d in departments)
{
    //add department node
    deptNode = new RadTreeNode()
    {
        Value = d.DeptId,
        Text = d.DepartmentName
    };
    tvwDepartmentCases.Nodes.Add(deptNode);

    //add staff nodes to department node
    var staffs = lstStaff
                    .ToList()
                    .Where(x => x.DeptId == d.DeptId);

    foreach (var s in staffs)
    {
        staffNode = new RadTreeNode()
        {
            Value = s.StaffID,
            Text = s.StaffName
        };
        deptNode.Nodes.Add(staffNode);
    }
}

Any comments are welcomed. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

时光无声 2024-10-03 06:24:58

我认为你所拥有的内容非常简单且易于阅读。它有性能改进的空间(可能可以忽略不计),但你会牺牲一些可读性。话虽如此,您对当前解决方案有哪些不满意而希望进一步重构?

I think what you have is pretty straightforward and easy to read. It has room for performance improvement (probably negligible) but you would sacrifice some readability. With that said, what don't you like about your current solution that you'd want further refactoring?

瑾夏年华 2024-10-03 06:24:58

我现在必须跑去上班,但我认为你在循环中做了太多查询。您应该在循环之前使用.ToLookup为员工组创建查找表,然后使用该查找表来查找每个部门的员工。

I must run to work now, but I think you did too much querying in the loop. You should use .ToLookup to create lookup table for groups of the staff before the loop, and use the lookup table to find the staff members in each department instead.

下雨或天晴 2024-10-03 06:24:58

我会写 -

foreach(var member in lstStaff)
 var deptNode = FindDeptNode(member.DeptId, member.DeptName);
 deptNode.Add(member.StaffId, member.StaffName);


.. // in FindDeptNode

if(!departmentsTreeView.ContainssNode(deptId, deptName))
 departmentsTreeView.Add(deptId, deptName);

return departmentsTreeView[deptId, deptName];

你可以将其转换为可编译的形式。

干杯。

I would write -

foreach(var member in lstStaff)
 var deptNode = FindDeptNode(member.DeptId, member.DeptName);
 deptNode.Add(member.StaffId, member.StaffName);


.. // in FindDeptNode

if(!departmentsTreeView.ContainssNode(deptId, deptName))
 departmentsTreeView.Add(deptId, deptName);

return departmentsTreeView[deptId, deptName];

you can convert this into the compilable form.

Cheers.

再可℃爱ぅ一点好了 2024-10-03 06:24:58

我会更进一步,从查询中创建人员节点。这在逻辑上对我来说更清晰,因为我们拥有此时创建节点所需的所有信息。不幸的是,不可能以干净、可读的方式在查询中创建部门节点。

var depts = from staff in lstStaff
            where !string.IsNullOrEmpty(staff.DepartmentName)
            group   //staff nodes
                new RadTreeNode  //each item in the group is a node
                {
                    Value = staff.StaffID,
                    Text = staff.StaffName,
                }
            by      //departments
                new
                {
                    staff.DeptID,
                    staff.DepartmentName,
                };
foreach (var dept in depts)
{
    var deptNode = new RadTreeNode
    {
        Value = dept.Key.DeptID,
        Text = dept.Key.DepartmentName,
    };
    deptNode.Nodes.AddRange(dept.ToArray());
    tvwDepartmentCases.Nodes.Add(deptNode);
}

I'd go a step further and create the staff nodes from within the query. It's logically clearer to me since we have all the information necessary to create the node at that point. Unfortunately it isn't possible to create the department node within the query in a clean, readable way.

var depts = from staff in lstStaff
            where !string.IsNullOrEmpty(staff.DepartmentName)
            group   //staff nodes
                new RadTreeNode  //each item in the group is a node
                {
                    Value = staff.StaffID,
                    Text = staff.StaffName,
                }
            by      //departments
                new
                {
                    staff.DeptID,
                    staff.DepartmentName,
                };
foreach (var dept in depts)
{
    var deptNode = new RadTreeNode
    {
        Value = dept.Key.DeptID,
        Text = dept.Key.DepartmentName,
    };
    deptNode.Nodes.AddRange(dept.ToArray());
    tvwDepartmentCases.Nodes.Add(deptNode);
}
So要识趣 2024-10-03 06:24:57

使用GroupBy 运算符,它很简单,而且看起来好多了。

var departmentGroups = lstStaff.GroupBy(staff => 
    new { staff.DeptID, staff.DepartmentName });

foreach (var department in departmentGroups)
{
    var deptNode = new RadTreeNode()
    {
        Value = department.Key.DeptID,
        Text = department.Key.DepartmentName
    };

    tvwDepartmentCases.Nodes.Add(deptNode);

    foreach (var staffMember in department)
    {
        var staffNode = new RadTreeNode()
        {
            Value = staffMember.StaffID,
            Text = staffMember.StaffName
        };
        deptNode.Nodes.Add(staffNode);
    }
}

它还降低了复杂性,因为您不需要遍历每个部门的整个 lstStaff 集合。

Use the GroupBy operator, it's easy and looks so much nicer.

var departmentGroups = lstStaff.GroupBy(staff => 
    new { staff.DeptID, staff.DepartmentName });

foreach (var department in departmentGroups)
{
    var deptNode = new RadTreeNode()
    {
        Value = department.Key.DeptID,
        Text = department.Key.DepartmentName
    };

    tvwDepartmentCases.Nodes.Add(deptNode);

    foreach (var staffMember in department)
    {
        var staffNode = new RadTreeNode()
        {
            Value = staffMember.StaffID,
            Text = staffMember.StaffName
        };
        deptNode.Nodes.Add(staffNode);
    }
}

It's also lower complexity because you don't need to iterate through the entire collection of lstStaff for each department.

为你拒绝所有暧昧 2024-10-03 06:24:57
var grping = from staff in lstStaff
             where !String.IsNullOrEmpty(staff.StaffName)
             group staff.StaffName by staff.DepartmentName;

foreach (var deptGrp in grping)
{
         //add a high level node using deptGrp.Key
         foreach (var staffName in deptGrp) 
               //add a lower level node
}
var grping = from staff in lstStaff
             where !String.IsNullOrEmpty(staff.StaffName)
             group staff.StaffName by staff.DepartmentName;

foreach (var deptGrp in grping)
{
         //add a high level node using deptGrp.Key
         foreach (var staffName in deptGrp) 
               //add a lower level node
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文