Entity Framework Join 3 Tables

Entity Framework Join 3 Tables

This is untested, but I believe the syntax should work for a lambda query. As you join more tables with this syntax you have to drill further down into the new objects to reach the values you want to manipulate.

var fullEntries = dbContext.tbl_EntryPoint
.Join(
dbContext.tbl_Entry,
entryPoint => entryPoint.EID,
entry => entry.EID,
(entryPoint, entry) => new { entryPoint, entry }
)
.Join(
dbContext.tbl_Title,
combinedEntry => combinedEntry.entry.TID,
title => title.TID,
(combinedEntry, title) => new
{
UID = combinedEntry.entry.OwnerUID,
TID = combinedEntry.entry.TID,
EID = combinedEntry.entryPoint.EID,
Title = title.Title
}
)
.Where(fullEntry => fullEntry.UID == user.UID)
.Take(10);

How to join 3 tables in entity framework 6

In the second Join clause, the item type is the anonymous type you just created in the previous (first) Join clause. In it the fields are defined as X and Y instead of t1 and t2, so these are the fields you should use.

entities.X
.Join(entities.Y, t1 => t1.ID, t2 => t2.ID1, (t1, t2) => new { X = t1, Y = t2 })
.Join(entities.Z.Where(p => p.param == 1), t2 => t2.Y.ID, t3 => t3.ID2, (t, t3) => new { X = t.X, Z = t3 })
.Select(u => u.X)
.Distinct();

(Edited to reflect the original SQL query, and explanation added)

Note: Where clause may be added at the end as well, but I used the current form because it is usually better to filter first (i.e. before joining, etc.).

How can I join multiple tables including a join-table (auto-generated by Entity Framework) using LINQ

Use the following query:

var query = 
from exams in _context.Exams
from students in exams.Class.Students
join people in _context.People on students.PersonId equals people.Id
where exams.Id == id
select new
{
Id = students.Id,
FullName = people.FirstName + " " + people.LastName
};

How to join 3 tables with linq

Can you try something similar to it please for joining part

from d in Duty
join c in Company on d.CompanyId equals c.id
join s in SewagePlant on c.SewagePlantId equals s.id
select new
{
duty = s.Duty.Duty,
CatId = s.Company.CompanyName,
SewagePlantName=s.SewagePlant.SewagePlantName
// other assignments
};

How to join multiple tables in entity framework

You can create a custom class to hold only ProductName and IsEnabled like this:

public class ProductEnableInfo
{
public string ProductName { get; set; }
public bool IsEnabled { get; set; }
}

Then you can change your method to this:

public IEnumerable<ProductEnableInfo> GetProducts()
{
return db.Products.Select(
x => new ProductEnableInfo
{
ProductName = x.ProductName,
IsEnabled = x.ProductDetail.IsEnabled
})
.ToList();
}

UPDATE:

You can filter by ProductID like this:

public IEnumerable<ProductEnableInfo> GetProducts(int product_id)
{
return db.Products
.Where(x => x.ProductID == product_id)
.Select(
x => new ProductEnableInfo
{
ProductName = x.ProductName,
IsEnabled = x.ProductDetail.IsEnabled
})
.ToList();
}

Join three tables with Entity Framework

You have navigation properties in your model. That means you don't need to worry about joins - they will be generated for you.

The equivalent LINQ query is simple as that:

var query = from hasta in db.Hasta
select new
{
hasta.HastaAdSoyad,
hasta.Randevu.RandevuTarihi,
hasta.Randevu.RandevuSaati
};
var result = query.ToList();

It's not clear why the original SQL query includes join to Doktor table.

If you really want joins (for some unknown reason), then the query is

var query = from hasta in db.Hasta
join randevu in db.Randevu on hasta.RandevuId equals randevu.RandevuId
join doktor in db.Doktor on randevu.DoktorId equals doktor.DoktorId
select new
{
hasta.HastaAdSoyad,
randevu.RandevuTarihi,
randevu.RandevuSaati
};


Related Topics



Leave a reply



Submit