Linq有多个连接

时间:2014-11-21 19:14:03

标签: c# sql-server linq join

我试图将以下SQL语句转换为Linq并且遇到多个连接时遇到问题 - 我似乎错过了一些东西。

            SELECT DISTINCT
                  Test1 = Table1.Column1,
                  Test2 = 1,
                  Test3 = Table1.Column2,
                  Test4 = Table1.Column5,
                  Test5 = Table1.Column6
            FROM Table1
            LEFT JOIN Table2 ON Table1.Column1 = Table2.Column1
            INNER JOIN Table3 ON Table1.Column3 = Table3.Column3 
            WHERE Table3.Column4 IN (1,2,6) 

到目前为止,Linq在这里:

var TestQuery = Table1_Collection.Select(x => new
            {
                Test1 = Table1.Column1,
                Test2 = 1,
                Test3 = Table1.Column2,
                Test4 = Table1.Column5,
                Test5 = Table1.Column6
            })
            [joins go here]
            .Where("where stuff goes here");

有什么想法吗?我不是那么寻求.Where的帮助,因为我是加入者。我不确定方法语法的格式。

3 个答案:

答案 0 :(得分:2)

你走了:

var results = Table3_Collection
    .Where(i => column4s.Contains(i.Column4))
    .Join(Table1_Collection, i => i.Column3, i => i.Column3, (i, j) => j)
    .Join(Table2_Collection, i => i.Column1, i => i.Column1, (i, j) => i)
    .Distinct(comparer);

在原始SQL查询中,您没有选择Table2中的任何列,因此您可以省略该连接。我把它包括在上面,但请随意将其删除。

此外,您的C#示例没有区别,但我将其包含在原始SQL查询中,并且很可能是您的意图。而且,请不要忘记实现自己的IEqualityComparer。以下是一个例子:

class Table1Comparer : IEqualityComparer<Table1>
{
    public bool Equals(Table1 x, Table1 y)
    {
        return x.Column1 == y.Column1
            && x.Column2 == y.Column2
            && x.Column3 == y.Column3
            && x.Column4 == y.Column4
            && x.Column5 == y.Column5
            && x.Column6 == y.Column6;
    }

    public int GetHashCode(Table1 obj)
    {
        return obj.GetHashCode();
    }
}

答案 1 :(得分:1)

以下是LINQ查询语法中原始SQL语句的示例。

List<int> vals = new List<int> {1,2,6};

var qry = from rec1 in Table1
          join rec2 in Table2 on rec1.Column1 equals rec2.Column2 into ljT2
          from rec2 in ljT2.DefaultIfEmpty()  //Handle left join
          join rec3 in Table3 on rec1.Column1 equals rec3.Column3
          where vals.Contains(rec3.Column4)
          select new {
                Test1 = rec1.Column1,
                Test2 = 1,
                Test3 = rec2 == null?null:rec2.Column2, //Must allow for rec2 to be null
                Test4 = rec3.Column5,
                Test5 = rec3.Column6
            }

qry = qry.Distinct();

答案 2 :(得分:1)

试试这个例子,希望你能帮忙

class Table1
{
    public int Id1 { get; set; }
    public string Column1 { get; set; }
}

class Table2
{
    public int Id2 { get; set; }
    public string Column2 { get; set; }
}

class Table3
{
    public int Id3 { get; set; }
    public string Column3 { get; set; }
}


    static void Main(string[] args)
    {
        var table1 = new List<Table1>();
        var table2 = new List<Table2>();
        var table3 = new List<Table3>();
        for (int i = 0; i < 10; i++)
        {
            table1.Add(new Table1 { Id1 = i, Column1 = "column1_table1_" + i });
            table2.Add(new Table2 { Id2 = i, Column2 = "column2_table2_" + i });
            table3.Add(new Table3 { Id3 = i, Column3 = "column3_table3_" + i });
        }

        var table1JoinTable2 = table1.Join(table2, t1 => t1.Id1, t2 => t2.Id2, (t1, t2) => new { Id = t1.Id1, Column1 = t1.Column1, Column2 = t2.Column2 } );
        var table1JoinTable2JoinTable3 = table1JoinTable2.Join(table3, t12 => t12.Id, t3 => t3.Id3, (t12, t3) => new { Id = t12.Id, Column1 = t12.Column1, Column2 = t12.Column2, Column3 = t3.Column3 });

        var result1 = table1JoinTable2JoinTable3.Single(t123 => t123.Id == 1);
        Console.WriteLine("Id={0} C1={1} C2={2} C3={3}", result1.Id, result1.Column1, result1.Column2, result1.Column3);
        // prints "Id=1 C1=column_table1_1 C2=column_table2_1 C3=column_table3_1"
    }