国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

C# Lambda && Linq

2019-11-17 02:53:33
字體:
來源:轉載
供稿:網友
C# Lambda && Linq
  1. Lambda表達式在C#3.0加入,它是一個匿名函數,可用于創建委托或者表達式樹類型,運算符為=>,讀作”goes to”,=>左側是變量,右側是表達式,變量類型可以自動推導
  2. Linq查詢操作由三個不同操作組成:示例代碼:
    • 獲取數據源,數據源可以使xml,SQL數據庫,Ado.net數據集,.Net集合中的數據等
    • 創建查詢,但并沒有執行查詢,只是用一個變量存儲查詢,查詢表達式包含三個字句:執行查詢:使用foreach語句循環訪問查詢變量時才會真正執行查詢,使用Count,Max,Average,First也會立即執行查詢,這些語句會隱式調用foreach
      • from:指定數據源
      • where:指定filter
      • select:指定返回數據
  3. [TestClass]    public class XmlLinqTest    {        PRivate static XElement doc;        [ClassInitialize]        public static void Initialize(TestContext context)        {            doc = XElement.Load("Books.xml");        }        [TestMethod]        public void QueryBook_Id_ReturnBook()        {                       IEnumerable<XElement> books=from el in doc.Elements()                          where el.Attribute("id").Value.Equals("1")                          select el;            XElement book = books.First();            Assert.AreEqual(book.Attribute("id").Value, "1");            Assert.AreEqual(book.Attribute("name").Value,"Linq");            Assert.AreEqual(book.Attribute("author").Value,"Tom");                    }            }
    <?xml version="1.0" encoding="utf-8" ?> <Books>  <Book id="1" name="Linq" author="Tom" />  <Book id="2" name="Lambda" author="Jerry" />  </Books>
  4. 查詢關鍵字:查詢語法在編譯時被轉換為方法調用,這些方法被稱為standard query Operators,包括Where,Select,GroupBy,Join,Max,Average等(都是擴展方法),可以直接調用這些方法替代查詢語法
    • orderby:對返回的數據進行排序
    • group:按指定的鍵對結果進行分組,如果要對分組的結果進行操作可以使用into創建一個可以使用的查詢符
    • 示例代碼:
      // custQuery 的類型是IEnumerable<IGrouping<string, Customer>>// IGrouping是group.. by語句的返回類型var custQuery =    from cust in customers    group cust by cust.City into custGroup    where custGroup.Count() > 2    orderby custGroup.Key    select custGroup;
    • join:聯接,join字句接受兩個數據源作為輸入,只能執行等聯接,也就是只能基于兩個鍵之間的相等關系進行匹配,分為如下幾種:
      • inner join:
      • group join:含有into字句,將會產生一個分組序列,該序列將左側數據源中的元素與右側數據源中的一個或多個元素相關聯,如果右側沒有與左側元素匹配的元素,則會產生一個空的集合
      • left outer join:在group join中使用DefaultEmpty方法,用于指定沒有匹配時,左側元素對應的元素,這樣左側數據源中的每個元素都會有對應的集合
      • 示例代碼:
        <?xml version="1.0" encoding="utf-8" ?><Authors>  <Author id="1" name="Tom" />  <Author id="2" name="Jerry"/>  <Author id="3" name="Jack"/></Authors><?xml version="1.0" encoding="utf-8" ?> <Books>  <Book id="1" name="Linq" authorId="1" />  <Book id="2" name="Lambda" authorId="2" />  <Book id="3" name="C#" authorId="1" /></Books>
        //BooksGroup是Books xml中滿足條件的element的集合
        var group = from author in authors.Elements()                            join book in books.Elements() on author.Attribute("id").Value                            equals book.Attribute("authorId").Value into BooksGroup                            where author.Attribute("id").Value.Equals("1")                            select new { Author = author.Attribute("name").Value, authorBooks = BooksGroup };
    • select:生成查詢結果
    • let:使用新的變量存儲子表達式的結果,可供后面語句繼續使用
  5. int[] numbers = { 5, 10, 8, 3, 6, 12};        //Query syntax:        IEnumerable<int> numQuery1 =             from num in numbers            where num % 2 == 0            orderby num            select num;        //Method syntax:        IEnumerable<int> numQuery2 = numbers.Where(num => num % 2 == 0).OrderBy(n => n);

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 永宁县| 博湖县| 密云县| 金寨县| 策勒县| 兴城市| 米泉市| 札达县| 昔阳县| 乡宁县| 辽源市| 仲巴县| 沙雅县| 上饶市| 太谷县| 万宁市| 临猗县| 进贤县| 金塔县| 赣榆县| 米易县| 循化| 亳州市| 通河县| 科技| 辽宁省| 拜泉县| 安阳市| 日喀则市| 化隆| 娄烦县| 景东| 阿克陶县| 开阳县| 德阳市| 洛扎县| 开原市| 大邑县| 乳山市| 诸暨市| 河池市|