I am to new to Entity Framework. Please help me with the following query.
I have 2 tables, Users and Companies and I need to make a right outer join on Users table.
Here is what I have right now.
List<Company> users = DbContext.Companies
.Where(p => !p.User.IsDeleted)
.Include(p=> p.User)
.OrderBy(p => p.User.FirstName)
.ToList();
Please help me out. TIA.
Relationship is User.id = Company.Companyid
Sample Data
Name Role Userid Companyid
Test1 Test1 User 210 210
Test2 Test2 User 1034 1034
Test3 Test3 Company Admin 2594 2594
Test4 Test4 Company Admin 5139 5139
Expected Result
Name Role Userid Companyid
Test1 Test1 User 210 210
Test2 Test2 User 1034 1034
Test3 Test3 Company Admin 2594 2594
Test4 Test4 Company Admin 5139 5139
Test5 Test5 Super Admin 1 Null
Here's my solution:
var usercompany = from u in dbcontext.User
join c in DbContext.Companies.Include(c=>c.User)
on u.id equals c.id into cu
from co in cu.DefaultIfEmpty()
select new
{
u.id,
co.id
};
var result= usercompany.ToList()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With