---
title: "LINQ Joins on multiple columns in Entity Framework"
description: "I came across a very annoying \"issue\" with LINQ joins (left joins) in Entity Framework where the compiler drove me nuts!"
updated: 2024-09-09
tags: ["C#", ".NET", "EF", "EF Core"]
canonical: "https://steven-giesel.com/blogPost/78753461-d80f-4f81-9b4d-6484066aa43e/linq-joins-on-multiple-columns-in-entity-framework"
---

# LINQ Joins on multiple columns in Entity Framework

I came across a very annoying "issue" with LINQ joins (left joins) in Entity Framework where the compiler drove me nuts!

## The original query

Basically I had a base query like this:
```csharp
var query = dbContext.Entity.Where(...);

var result = from e in query
             join j in dbContext.JoinEntity on new { e.Id, e.OtherId } equals new { j.WId, j.OtherId } into group
             from g in group.DefaultIfEmpty()
             select new { e, g };
```

This query leads to a nasty compiler error:

```no-class
The type arguments cannot be inferred from the query. Candidates are: ... (very long text here)
```

## The solution
The problem is that I used multiple columns as join and **ALL** of them have to have the same name, otherwise the compiler will not be able to infer the type arguments (???). So the easy fix is something along the lines of:

```csharp
var result = from e in query
             join j in dbContext.JoinEntity on new { WId = e.Id, OtherId= e.OtherId } equals new { WId = j.WId, OtherId = j.OtherId } into group
             from g in group.DefaultIfEmpty()
             select new { e, g };
```

So both columns have to have the same name in the anonymous types. And yes, I could have just write plain SQL (which I also do), but that is besides the point.