C
C#13mo ago
dhoni7

EF/Linq Left joining two tables after filtering the first table

I need to perform left join Table1 on Table2, before I do that I have to filter table1 records with employee id, I right now have
List<Department> Departments, //this holds all my departments
List<Department> Departments, //this holds all my departments
I tried this but It says that Ienumeable does not have defintion for IQueryable, can someone please help how to perform this join operation. Thanks
var result = (from department in Departments
from employeeId in department.Employees.Select(e => e.EmployeeId)
join at in dbContext.Table1 on employeeId equals at.EmployeeId
join at2 in dbContext.Table2 on new { at.SomeField1, at.SomeField2 } equals new { at2.SomeField1, at2.SomeField2 } into at2Group
from at2Data in at2Group.DefaultIfEmpty()
select new
{
DepartmentId = department.Id,
FieldFromTable1 = at.SomeField,
FieldFromTable2 = at2Data.SomeField
}).ToListAsync();
var result = (from department in Departments
from employeeId in department.Employees.Select(e => e.EmployeeId)
join at in dbContext.Table1 on employeeId equals at.EmployeeId
join at2 in dbContext.Table2 on new { at.SomeField1, at.SomeField2 } equals new { at2.SomeField1, at2.SomeField2 } into at2Group
from at2Data in at2Group.DefaultIfEmpty()
select new
{
DepartmentId = department.Id,
FieldFromTable1 = at.SomeField,
FieldFromTable2 = at2Data.SomeField
}).ToListAsync();

public class Employee {
public int Id;
public string Name;
}

public class Department{
public int Id;
public List<Employee> Employees;
}


public class Table1{
public int Id;
public int EmployeeId;
public string SomeField1;
public string SomeField2;
public string SomeField3;
public string SomeField4;
public string SomeField5;
public string SomeField6;
}

public class Table2{
public int Id;
public string SomeField1;
public string SomeField2;
public string SomeField3;
public string SomeField4;
public string SomeField5;
public string SomeField6;
}
public class Employee {
public int Id;
public string Name;
}

public class Department{
public int Id;
public List<Employee> Employees;
}


public class Table1{
public int Id;
public int EmployeeId;
public string SomeField1;
public string SomeField2;
public string SomeField3;
public string SomeField4;
public string SomeField5;
public string SomeField6;
}

public class Table2{
public int Id;
public string SomeField1;
public string SomeField2;
public string SomeField3;
public string SomeField4;
public string SomeField5;
public string SomeField6;
}
18 Replies
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
dhoni7
dhoni7OP13mo ago
I m sorry to ask what is C# query syntax.
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
dhoni7
dhoni7OP13mo ago
thanks I will look into it
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
dhoni7
dhoni7OP13mo ago
yes I am @TeBeCo even after I use c# syntax, I am still getting the dame exception
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
dhoni7
dhoni7OP13mo ago
OhNo it is converting to Ienumerable but it is supposed to be IQueryAble
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
dhoni7
dhoni7OP13mo ago
I am working on Office machine, I am getting error at ToListAsync() it says that IEnumerable<> doesn’t contain a defintion for ToListAsync()
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
dhoni7
dhoni7OP13mo ago
if you Departments here, It is in memory do you think that is causing problem
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
dhoni7
dhoni7OP13mo ago
sure I will
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
dhoni7
dhoni7OP13mo ago
hmm…do you think other way to make this achievable? I am sorry to ask you this way, but I am not able to get any idea
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
dhoni7
dhoni7OP13mo ago
Sure I will do Hi @TeBeCo , I am also in VC-0 can I share my screen. Thanks
Want results from more Discord servers?
Add your server