I need a Linq to SQL for which I'll get Company name and Reporting Person's name as input.
From the below table I need to query and I need to get the values of the children and grand children and their descendants too.
Please find the sample data below:
For Ex:
if I get Company name = 'Intech' and Reporting Person's name = 'Aashiq'
I need the below values
'Vignesh','Aashiq'
'KK','Aashiq'
'Dhanesh','Aashiq'
'Sanjeev','KK'
'Periyannan','Antony'
'Karthi','Antony'
'Prakash','Dhanesh'
'XXXX', 'Prakash'
'YYYY', 'Prakash'
Please help
CREATE TABLE Table1
(
CompanyName VARCHAR(20),
EmployeeName VARCHAR(20),
ReportingPersonName VARCHAR(20)
)
insert into Table1 Select ' Intech','Prem','Gandhi'
insert into Table1 Select ' Intech','JK', 'Gandhi'
insert into Table1 Select ' Intech','Gobind','Prem'
insert into Table1 Select ' Intech','KP','Prem'
insert into Table1 Select ' Intech','Venkat','Goind'
insert into Table1 Select ' Intech','Niyam','Goind'
insert into Table1 Select ' Intech','Aashiq','KP'
insert into Table1 Select ' Intech','James','KP'
insert into Table1 Select ' Intech','Linu','Venkat'
insert into Table1 Select ' Intech','Mathi','Niyam'
insert into Table1 Select ' Intech','Karthik','Niyam'
insert into Table1 Select ' Intech','Vignesh','Aashiq'
insert into Table1 Select ' Intech','KK','Aashiq'
insert into Table1 Select ' Intech','Antony','James'
insert into Table1 Select ' Intech','Dhanesh','Aashiq'
insert into Table1 Select ' Intech','Anitha','Linu'
insert into Table1 Select ' Intech','Manu','Mathi'
insert into Table1 Select ' Intech','Babu','Mathi'
insert into Table1 Select ' Intech','Kavya','Karthik'
insert into Table1 Select ' Intech','Subbu','Karthik'
insert into Table1 Select ' Intech','Sanjeev','KK'
insert into Table1 Select ' Intech','Periyannan','Antony'
insert into Table1 Select ' Intech','Karthi','Antony'
insert into Table1 Select ' Intech','Prakash','Dhanesh'
insert into Table1 Select ' Intech','XXXX', 'Prakash'
insert into Table1 Select ' Intech','YYYY', 'Prakash'
Loading
Lawrence PondPosted Nov 26, 2014, 2:52 AM
This has been modified by me from a earlier question that was answered by the knowledgeable developer VULPES, This answer should satisfy you needs,
Note a couple of problems exist in your table it would be easy to create a circular relationship referring to one person reports to another and the other reports back to the person, in the following example level keeps this from happening if you only want to go 3 levels deep then pass in 3, This example was written using linq to objects however it really easy for you to take it and build a linq to sql dll to reference,
-----------------------------------------------------
output when you want to see the first 3 levels that report to Gandhi
Gandhi-Gandhi
Gobind-Gobind
Gobind-Gobind
JK-JK
KP-KP
KP-KP
Prem-Prem
---------------------------------------------------
employees = new List
GetChildren(IEnumerable persons, string id, int level)
();
GetChildren2(IEnumerable persons, string id, int level)(); results = children;
void main()
{
List
{
new Employee("Gandhi", "Gandhi", null),
new Employee("Prem", "Prem", "Gandhi"),
new Employee("JK", "JK", "Gandhi"),
new Employee("Gobind", "Gobind", "Prem"),
new Employee("KP", "KP", "Prem"),
new Employee("Venkat", "Venkat", "Goind"),
new Employee("Niyam", "Niyam", "Goind"),
new Employee("Aashiq", "Aashiq", "KP"),
new Employee("James", "James", "KP"),
new Employee("Linu", "Linu", "Venkat"),
new Employee("Mathi", "Mathi", "Niyam"),
new Employee("Karthik", "Karthik", "Niyam"),
new Employee("Vignesh", "Vignesh", "Aashiq"),
new Employee("Gobind", "Gobind", "Prem"),
new Employee("KP", "KP", "Prem"),
new Employee("Venkat", "Venkat", "Goind"),
new Employee("Niyam", "Niyam", "Goind"),
new Employee("Aashiq", "Aashiq", "KP"),
new Employee("James", "James", "KP"),
new Employee("Linu", "Linu", "Venkat"),
new Employee("Karthik", "Karthik","Niyam"),
new Employee("Vignesh", "Vignesh","Aashiq"),
new Employee("KK", "KK","Aashiq"),
new Employee("Antony", "Antony","James"),
new Employee("Dhanesh", "Dhanesh","Aashiq"),
new Employee("Anitha", "Anitha","Linu"),
new Employee("Manu", "Manu","Mathi"),
new Employee("Babu", "Babu","Mathi"),
new Employee("Kavya", "Kavya","Karthik"),
new Employee("Subbu", "Subbu","Karthik"),
new Employee("Sanjeev", "Sanjeev","KK"),
new Employee("Periyannan", "Periyannan","Antony"),
new Employee("Karthi", "Karthi","Antony"),
new Employee("Prakash", "Prakash","Dhanesh"),
new Employee("XXXX", "XXXX", "Prakash"),
new Employee("YYYY", "YYYY", "Prakash")
};
var children = GetChildren(employees, "Gandhi", 3);
foreach(Employee child in children)
{
Console.WriteLine("{0}-{1}", child.Id, child.Name);
}
Console.Read();
}
static IEnumerable
{
if (level < 1) level = 1;
var parent = persons.Where(p => p.Id.Equals(id));
if (parent.Count() == 0) return Enumerable.Empty
if (level == 1) return parent;
var children = GetChildren2(persons, parent.First().Id, level - 1);
if (children.Count() == 0) return parent;
return parent.Union(children).OrderBy(p => p.Id);
}
static IEnumerable
{
var children = persons.Where(p =>p.ParentId != null && p.ParentId.Equals( id));
if (children.Count() == 0) return Enumerable.Empty
if (level == 1) return children;
IEnumerable
foreach(Employee c in children)
{
var grandChildren = GetChildren2(persons, c.Id, level - 1);
if(grandChildren.Count() > 0)
{
results = results.Union(grandChildren);
}
}
return results;
}
public class Employee
{
public string Id {get; set;}
public string Name {get; set;}
public string ParentId {get; set;}
public Employee(string id, string name, string parentId)
{
Id = id;
Name = name;
ParentId = parentId;
}
}
// Define other methods and classes here
Rahul SinghPosted Oct 29, 2014, 1:58 PM
Although, If you find better solution, Please let me know.