linq to entities left outer join

时间:2010-02-01 05:01:43

标签: c# linq-to-entities left-join

我正在努力向实体留下外连接。我有两个实体(表格):

Listings 
{
    ListingID,
    MakeID (nullable)
}

Makes
{
    MakeID,
    Description
}

我想在LINQ中写这样的东西:

select listings.listingID
,listings.makeid
, IsNull(makes.Description, 'NA')
from listings
left outer join makes
on listings.makeid = makes.makeid

4 个答案:

答案 0 :(得分:5)

以下是实现左连接的解决方案。在其他资源方面,我真的建议尝试使用linq pad:http://www.linqpad.net/这对Linq来说是一个很好的学习工具。

// Listing class/container/table
public class Listing
{
    public string ListingID {get;set;}
    public Int32? MakeID {get;set;}
}

// Make class/container/table
public class Make
{
    public Int32 MakeID {get;set;}
    public string Description {get;set;}
}

public class Main
{
    public static void LinqMain()
    {
        // Populate the listing table with data
        List<Listing> listings = new List<Listing>()
        {
            new Listing() { ListingID = "Test 1", MakeID = 1 },
            new Listing() { ListingID = "Test 2", MakeID = 1 },
            new Listing() { ListingID = "No Make", MakeID = null },
            new Listing() { ListingID = "Test 3", MakeID = 3 },
            new Listing() { ListingID = "Another Makeless", MakeID = null }
        };

        // Populate the makes table with data
        List<Make> makes = new List<Make>()
        {
            new Make() { MakeID = 1, Description = "Make 1"},
            new Make() { MakeID = 2, Description = "Make 2"},
            new Make() { MakeID = 3, Description = "Make 3"},
            new Make() { MakeID = 4, Description = "Make 4"}
        };

        // Return the left join on Make Id
        var result = from l in listings

                     // These two lines are the left join. 
                     join leftm in makes on l.MakeID equals leftm.MakeID into leftm
                     from m in leftm.DefaultIfEmpty()

                     // To ensure the select does not get bogged down with too much logic use the let syntax
                     let description = m == null ? "NA" : m.Description

                     select new { l.ListingID, l.MakeID, description };


    }

结果变量将包含:

  1. {ListingID =“Test 1”,MakeID = 1,description =“Make 1”}
  2. {ListingID =“Test 2”,MakeID = 1,description =“Make 1”}
  3. {ListingID =“No Make”,MakeID = null,description =“NA”}
  4. {ListingID =“Test 3”,MakeID = 3,description =“Make 3”}
  5. {ListingID =“Another Makeless”,MakeID = null,description =“NA”}

答案 1 :(得分:2)

任何告诉你使用.DefaultIfEmpty()作为LINQ to Entities的外连接的一部分的人实际上并没有尝试过它! Tt根本不起作用 - 至少在.NET 3.5 SP1中是这样。

This blogger告诉你应该如何实际做到这一点。从本质上讲,.NET默认情况下在LINQ to Entities 中进行外连接,所以你应该省略.DefaultIfEmpty()。对于多个外部联接,您必须嵌套查询组以保持其上下文清晰。

答案 2 :(得分:0)

http://oddiandeveloper.blogspot.com/2008/12/testable-left-outer-join-in-linq-to.html

这应该有所帮助,这是我前一段时间发表的一篇博文,应该仍然具有相关性,也可能有助于提高可测试性。

在生成实体模型时,还要确保您的外键已就位,它将帮助您设置依赖项。

答案 3 :(得分:0)

不要在开发机器前面检查,但也许这样的事情?

var x = from l in listings
    join m in makes on l.makeid equals m.makeid into g
    from ma in g.DefaultIfEmpty()
    select new 
    {
        l.listingID, 
        l.makeid, 
        (ma.Description == null ? "NA" : ma.Description)
    };

如果您遇到任何问题,请告诉我,我会检查我的工作电脑。