I have 2 tables and i want to match up 2 Id values.

First table

* Id - 1, 2, 3, 4, 5
* DepartmentId - 2, 4, 5, 2, 1

Second table

* Id- 1, 2, 10, 30, 40

I want to match up first table's Id's with second table's Id's so i can get DepartmentId values.

I need to get this virtual result:

* Id- 1, 2, 10, 30, 40
* DepartmentId -2, 4, null, null, null

Here is my code:

```
       for (int i = 0; i < model1.Count(); i++)
    { 
model1[i].DepartmentId= model2.FirstOrDefault(k => k.Id== model1[i].Id).DepartmentId; 
      }

```

I get this error:

> 
> An exception of type 'System.NullReferenceException' occurred in
>  IYP.UserInterfaceLayer.dll but was not handled in user code
> 
> 
> 

I think loop fails because of it can't find 10, 30, 40 Id values. If my Id values are same in 2 tables( Id = 1,2,3,4,5) loop works.

How can i do this with Linq?