[Solved] Write an INNER JOIN in LINQ


I think it is easier to translate SQL using query comprehension syntax instead of lambda syntax.

General rules:

  1. Translate inner queries into separate query variables
  2. Translate SQL phrases in LINQ phrase order
  3. Use table aliases as range variables, or if none, create range
    variables from table names abbreviations
  4. Translate IN to Contains
  5. Translate SQL functions such as DISTINCT or SUM into function calls
    on the entire query.
  6. Create anonymous objects for multi-column grouping or joining

Using these rules, you should get something like:

var ans = from t in tblTeamType
          join p in ProjInformation on t.team_id equals p.teamId
          join pj in projProject on p.projectId equals pj.projectId
          join ph in ProjInfoAdditionalHrs on new { p.teamId, p.Id } equals new { ph.teamId, ph.proJinfold }
          select new {
              ph.Id,
              projInfoId = p.Id,
              ph.Title,
              ph.AdditionalHours,
              ph.AdditionalCost,
              ph.InsertDate,
              ph.InsertBy,
              ph.LastUpdateDate,
              ph.LastUpdateBy,
              ph.TeamId,
              ph.ProjInfoId
          };

solved Write an INNER JOIN in LINQ