1.21 jigowatts

Great Scott!

LINQ to EntitiesでLEFT OUTER JOIN

概要

似ているようで結構違う。SQLLINQの比較。
複雑なクエリはなるべく避けて生きよう。

SQL
SELECT
    x.ID,
    x.ProductName,
    y.SubProductName,
    x.UpdateDate,
    x.UpdateUser
FROM
    Product x
    LEFT OUTER JOIN
    SubProduct y
    ON x.ID = y.ID
ORDER BY
    x.ID
LINQ to Entities
using (MyEntities context = new MyEntities()) 
{
    var query = from x in context.Product
                 join y in context.SubProduct
                 on x.ID equals y.ID into xy
                 from z in xy.DefaultIfEmpty()
                 orderby x.ID
                 select new 
                 {
                    x.ID,
                    x.ProductName,
                    z.SubProductName,
                    x.UpdateDate,
                    x.UpdateUser
                 };
}