重构这段代码
我正在尝试根据来自各个部门的工作人员列表(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我认为你所拥有的内容非常简单且易于阅读。它有性能改进的空间(可能可以忽略不计),但你会牺牲一些可读性。话虽如此,您对当前解决方案有哪些不满意而希望进一步重构?
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?
我现在必须跑去上班,但我认为你在循环中做了太多查询。您应该在循环之前使用
.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.我会写 -
你可以将其转换为可编译的形式。
干杯。
I would write -
you can convert this into the compilable form.
Cheers.
我会更进一步,从查询中创建人员节点。这在逻辑上对我来说更清晰,因为我们拥有此时创建节点所需的所有信息。不幸的是,不可能以干净、可读的方式在查询中创建部门节点。
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.
使用
GroupBy
运算符,它很简单,而且看起来好多了。它还降低了复杂性,因为您不需要遍历每个部门的整个
lstStaff
集合。Use the
GroupBy
operator, it's easy and looks so much nicer.It's also lower complexity because you don't need to iterate through the entire collection of
lstStaff
for each department.