C# Linq Join & Lambda Join

1. Linq Join Example:

    var result = from  c in T_Classes join 
    s in T_Students on c.ClassID equals s.ClassID
    where c.ClassName == "一年級"
    orderby c.ClassID descending
    select new {c.ClassName,s.StudentName};
    result.Dump();
Result:

Linq就像大家說的很像T-SQL的倒裝句。

2.Lambda Join Example:

    var Lambdajoin = T_Classes.Join(T_Students, //第一個參數為 要加入的資料來源
    c => c.ClassID,//主表要join的值
    s => s.ClassID,//次表要join的值
    (c,s) => new  // (c,s)代表將資料集合起來
    {
        ClassName = c.ClassName,
        Name = s.StudentName,
        ID = c.ClassID
    }).OrderBy(cs => cs.ID).Where(cs => cs.ClassName =="一年級");//排序及查詢條件
    Lambdajoin.Dump();
Result:

 

Lambda的話語法就比較特別一點還真的需要一些時間適應一下。

猜你喜欢

转载自www.cnblogs.com/IIXS/p/12193938.html