Eric Lei
BAN USERCan this work, using Recursion + Linq. Just worrying about the efficiency.
public class Employee
{
public string Alias { get; set; }
public string Name { get; set; }
/* other properties for employee details*/
public List<Employee> DirectReports { get; set; }
}
static IEnumerable<Employee> GetChildren(Employee e)
{
if (e.DirectReports != null)
{
foreach (var rChild in e.DirectReports.SelectMany(child => GetChildren(child)))
{
yield return rChild;
}
}
else
{
yield return e;
}
}
// Test code:
Employee gm1 = new Employee { Alias = "e1", Name = "GM1" };
Employee gm2 = new Employee { Alias = "e2", Name = "GM2" };
Employee gm3 = new Employee { Alias = "e3", Name = "GM3" };
Employee gm4 = new Employee { Alias = "e4", Name = "GM4" };
Employee vp1 = new Employee { Alias = "e5", Name = "VP1", DirectReports = new List<Employee> { gm1, gm2 } };
Employee vp2 = new Employee { Alias = "e6", Name = "VP2", DirectReports = new List<Employee> { gm3, gm4 } };
Employee ceo = new Employee { Alias = "e7", Name = "CEO", DirectReports = new List<Employee> { vp1, vp2} };
IEnumerable<Employee> result =
from e in GetChildren(ceo)
where e.Alias == "e2"
select e;
if (result == null || result.Count() == 0)
Console.WriteLine("Not found");
else
Console.WriteLine(result.First().Name);
// Output: GM2
A sample for << using C#
public static string operator <<(Operator op, int num)
{
int len;
if (string.IsNullOrWhiteSpace(op.Str) || num >= op.Str.Length)
{
len = 0;
}
else
{
len = op.Str.Length - num;
}
return op.Str.Substring(len) + op.Str.Substring(0, len);
}
public class Operator
{
public string Str { get; set; }
}
My understanding, actually some common senseļ¼
- Eric Lei April 30, 20141. What's the class used for? You cannot include everything or too much in one class.
2. You are going to create an abstract class or a class inheriting another?
3. Namespace, Constructors, Members, Properties, Methods, etc.
4. The role of this class in your design/architecture/model.
5. ...