專案使用中Linq使用總結

2021-05-24 05:49:43 字數 1568 閱讀 6249

本文旨在和網友分享linq在專案中的實踐,曾經我參與過的專案都能看見linq的影子。(linqtosql、linqtostring、linqtoxml、linqtoentity 等等...)個人認為linq是近年來微軟推出的非常成功的一項技術。比如,當我熟悉linq之後,再用sql寫商業邏輯時,感覺已經不再適應了。如果您還沒有系統的研究過linq,希望本文可以給您帶來幫助。

一、linqtoxml

在測試工程內加入xml檔案:

用linq操作xml檔案,可以增刪改查xml,示例**如下:

二、linqtoentity

用ef操作northwind 資料庫:

注,生成的sql語句分別為:

inner join :select /r/n1 as [c1], /r/n[extent1].[companyname] as [companyname], /r/n[extent2].[orderdate] as [orderdate]/r/nfrom  [dbo].[customers] as [extent1]/r/ninner join [dbo].[orders] as [extent2] on [extent1].[customerid] = [extent2].[customerid]

outer join:select /r/n1 as [c1], /r/n[extent1].[companyname] as [companyname], /r/n[extent2].[orderdate] as [orderdate]/r/nfrom  [dbo].[customers] as [extent1]/r/nleft outer join [dbo].[orders] as [extent2] on [extent1].[customerid] = [extent2].[customerid]

三、linqtofile

linq操作磁碟檔案:

四、linqtoarray

linq運算元組:

小結

以上四點對linq的總結相對比較全面了,為了讓我們的**更優美,更便於維護,還有理由不用linq麼?

專案使用中Linq使用總結

本文旨在和分享linq在專案中的實踐,曾經我參與過的專案都能看見linq的影子。linqtosql linqtostring linqtoxml linqtoentity 等等.個人認為linq是近年來微軟推出的非常成功的一項技術。比如,當我熟悉linq之後,再用sql寫商業邏輯時,感覺已經不再適應...

Linq使用Group By經驗總結

學習linq時,經常會遇到linq使用group by問題,這裡將介紹linq使用group by問題的解決方法。1.計數 var q from p in db.products group p by p.categoryid into g select new 語句描述 linq使用group b...

LinQ使用積累

1.用linq查詢datatable中的行是否重複 intcount1 from p indt.asenumerable select p distinct count intcount2 from p indt.asenumerable select p count if count1 count...