我發現我使用這個模式很多在LINQ:這是什麼LINQ的方法語法,它是一個連接,如果不是,那麼它是什麼?
class Thing
{
public int ID { get; set; }
public int ColorID { get; set; }
}
class Color
{
public int ID { get; set; }
public string Description { get; set; }
}
static void Main()
{
var things = new List<Thing> { new Thing { ID = 1, ColorID = 1 }, new Thing { ID = 2, ColorID = 1 }, new Thing { ID = 3, ColorID = 2 }, new Thing { ID = 4, ColorID = 1 } };
var colors = new List<Color> { new Color { ID = 1, Description = "red" }, new Color { ID = 2, Description = "green" }, new Color { ID = 3, Description = "blue" } };
var joined = (from thing in things
from color in colors
where thing.ColorID == color.ID
select new { ID = thing.ID, Color = color.Description }).ToArray();
foreach (var thing in joined)
{
Console.WriteLine("(" + thing.ID + ", " + thing.Color + ")");
}
//Writes:
//(1, red)
//(2, red)
//(3, green
//(4, red)
}
它的核心,查詢語法的四大行,感覺非常像一個INNER JOIN
,我可能會在TSQL寫的,但是當我看着examples of LINQ query syntax for joins他們使用單詞join
,而上面的LINQ沒有。
上面的LINQ正在執行的'連接'是什麼,我將如何在LINQ方法語法中重寫它?
謝謝約翰。我通過專門編寫查詢語法來開始LINQ,但最近我一直在交換方法語法,因爲它通常更清晰,更像C#一樣,但在這種情況下,它更簡單得多,儘管它有助於解釋發生了什麼。 – dumbledad 2014-11-25 14:25:03
我正要回答它執行內部循環連接。語法非常多,如何在SQL-89中編寫內部連接。但你說它不是。 – Magnus 2014-11-25 14:25:34
@Magnus:它可能被*翻譯成LINQ to SQL的「內部循環連接」,當然...... – 2014-11-25 14:27:46