C
C#7mo 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 User7mo ago
Message Not Public
Sign In & Join Server To View
dhoni7
dhoni77mo ago
I m sorry to ask what is C# query syntax.
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
dhoni7
dhoni77mo ago
thanks I will look into it
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
dhoni7
dhoni77mo ago
yes I am @TeBeCo even after I use c# syntax, I am still getting the dame exception
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
dhoni7
dhoni77mo ago
OhNo it is converting to Ienumerable but it is supposed to be IQueryAble
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
dhoni7
dhoni77mo 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 User7mo ago
Message Not Public
Sign In & Join Server To View
dhoni7
dhoni77mo ago
if you Departments here, It is in memory do you think that is causing problem
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
dhoni7
dhoni77mo ago
sure I will
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
dhoni7
dhoni77mo 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 User7mo ago
Message Not Public
Sign In & Join Server To View
dhoni7
dhoni77mo 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
More Posts
WPF Custom controls/User controls binding issues and total confusionneed to create a reusable WPF control for displaying plugin settings. There is a defined set of settEF/Linq select all columns of main table, but only some cols of joined table?I find myself often wanting to select all of a table, but very limited columns of joined tables. IsX:Bind not working troubleI'm trying to bind some text into my textblock from another document but I can't manage to do it, c✅ Caesar Cipher Code Not Returning AnythingThis is my code. I'm not understanding why, when I build this, nothing is outputted to the Console. ✅ Where to store sensitive information when deploying to the production?Hello everyone, When developing an application locally, we will store all the sensitive information Attempting to get the file name from a REST api download linkI have a method that that recive a zip file from a REST api download link. However, the method that first time text adventureI started writing some code (based on my classmates' and chat gpt's assistance) because I wanted to Backend For Frontend Deployment on AzureAnyone got experience deploying a Backend For Frontend Project (Vue.js with ASP.NET Core) on Azure? ✅ How to Fix Merge Conflicts? Beginner - ConfusedI have no idea what I've done. I tried to revert a commit through GitHub Desktop and now this has haIs anyone able to turn a C#/.net application into a Local Hosted Website?I have an application that id like to put onto a locally hosted Website.