Question: I'm using VS2008 (.NET 3.5) and Entity Framework over SQL Server. I'm hoping to get the value of a foreign key column without using the `Include()` method to link up the table.

For example, the simple Author has many Books sample.

```
var book = context.Books.First();
int authorId = book.Author.AuthorId;

```

In this scenario, an exception is thrown, because Author is null. I'm looking to just get the author ID, but don't need the rest of the Author object. There is no `AuthorId` property on the Book class.

I'm sure there's a way to do this, probably by editing the EDMX directly, or maybe through the designer - any ideas how to get started?

Thanks

Answer: 
`PIVOT`ing is neither easy (nor nice) to do. I prefer using `CASE`:

```
SELECT d.data_timestamp
     , SUM( CASE WHEN data_id =  1 THEN data_value ELSE 0 END ) AS 'input_1'
     , SUM( CASE WHEN data_id =  2 THEN data_value ELSE 0 END ) AS 'input_2'
     ...
     , SUM( CASE WHEN data_id = 20 THEN data_value ELSE 0 END ) AS 'input_20'
FROM data 
GROUP BY data_timestamp
ORDER BY data_timestamp ASC

```

but `IF` works in MySQL as well:

```
SELECT d.data_timestamp
     , SUM( IF(data_id =  1, data_value, 0) ) AS 'input_1'
     , SUM( IF(data_id =  2, data_value, 0) ) AS 'input_2'
     ...
     , SUM( IF(data_id = 20, data_value, 0) ) AS 'input_20'
FROM data 
GROUP BY data_timestamp
ORDER BY data_timestamp ASC

```

Alternatively, you can use a 20-level `JOIN`:

```
SELECT d.data_timestamp
     , d01.data_value AS 'input_1'
     , d02.data_value AS 'input_2'
     ...
     , d20.data_value AS 'input_20'
FROM
  ( SELECT DISTINCT d.data_timestamp
    FROM data
  ) AS d 
  LEFT JOIN data AS d01
    ON d01.data_timestamp = d.data_timestamp
    AND d01.data_id =  1
  LEFT JOIN data AS d02
    ON d02.data_timestamp = d.data_timestamp
    AND d02.data_id =  2
  ...                                   --- 20 JOINs
  LEFT JOIN data AS d20
    ON d20.data_timestamp = d.data_timestamp
    AND d20.data_id = 20
ORDER BY d.data_timestamp ASC

```
Just join the table to itself!

```
SELECT dt1.data_timestamp, dt1.input_1, dt2.input_2
FROM data_timestamp dt1
JOIN data_timestamp dt2 
    on dt1.data_timestamp = dt2.data_timestamp 
    and dt2.input_1 is null
WHERE dt1.input_2 is null;

```

Note that this query assumes input\_2's value are present for every input\_1 value. If that's not that case, use `LEFT JOIN` or `CROSS JOIN` etc