參考文檔:https://docs.microsoft.com/en-us/dotnet/csharp/linq/perform-inner-joins
1:内連接配接join
var query = from emp in t.Employees
join ed in t.EmployeeDetails on emp.Id equals ed.EDId
select new { emp.Id, emp.Name, final?.Address };
2:左連接配接
var query = from emp in t.Employees
join ed in t.EmployeeDetails on emp.Id equals ed.EDId into tmp
from final in tmp.DefaultIfEmpty()
select new { emp.Id, emp.Name, final?.Address };
使用 join x in y on t.id equals x.id into temp from z in temp.DefaultIfEmpty()
3:右連接配接
var query = from ed in t.EmployeeDetails
join emp in t.Employees on emp.Id equals ed.EDId
select new { emp.Id, emp.Name, final?.Address };
備注:?. 表示對象不為空執行後面
關于C#中?的使用含意:
1:三元表達式(邏輯判斷)
string name=class==1?"班級1":“其它”
2:連接配接對象(空合并運算符)
string name = null;
string name2 = "jition";
string c = name ?? name2 ;
//如果name 為null,就傳回name2 (??左邊為null則傳回??右邊,??左邊不為null則傳回??左邊);
3:通路修飾符(允許為null的資料類型)
一般基本類型是不允許null
如:
private int ? age{get;set}
private bool ? status{get;set};
private DateTime? CreateTime{get;set};
4: NULL檢查運算符(?.)
對象允許為null,不會抛出空指針異常
int? firstX = null;
if (points != null)
{
var first = points.FirstOrDefault();
if (first != null)
firstX = first.X;
}
在C# 6.0中,引入了一個 ?. 的運算符,前面的代碼可以改成如下形式: