How to join 3 tables with linq

asp.net-mvc c# entity-framework linq

Question

I am trying to join 3 tables in a query with Linq to get data from all 3 tables. Below is an image of the table schemes:

enter image description here

The query should select: SewagePlantName, CompanyName and Duty

In addition I need to restricts the SewagePlantId to a list of Ids that are given as:

            var sewagePlantIds = UnitOfWork.GetAll<UserGroup>()
            .Where(group => group.Id == webAppPrincipal.GroupId)
            .SelectMany(group => group.SewagePlantId).Select(sewageplant => sewageplant.Id).ToList();

I have difficulties with the order of joining the 3 tables and where/how to restrict the SewagePlantId to the given list.

1
4
2/8/2019 10:01:15 PM

Accepted Answer

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
  };
12
3/10/2020 8:24:51 AM

Popular Answer

var obj = from trns in context.tblPartyRegistrations
          join st in context.tblSellingTrans
          on trns.PartyRegId equals st.Fk_PartyRegId
          join pt in context.tblPartyRemainings
          on trns.PartyRegId equals pt.fk_PartyId
          select new
          {
              trns.Name,
              trns.PhoneNo,
              trns.Address,
              st.RecivedAmount,
              st.Date,
              st.CustomerType,
              st.MilkRate,
              st.Mltr,
              st.Mkg,
              st.NtAmnt,
              st.RemaningAmount,
              pt.Remainingammount
          };


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow