您好,登錄后才能下訂單哦!
小編給大家分享一下高效的LINQ語句有哪些,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
public class Order { public int Id { get; set; } public decimal Amount { get; set; } public string CustomerName { get; set; } public string Status { get; set; } }
public class Person { public string Name { get; set; } public int Age { get; set; } }
class Program { //AutoResetEvent 允許線程通過發信號互相通信。通常,此通信涉及線程需要獨占訪問的資源。 //線程通過調用 AutoResetEvent 上的 WaitOne 來等待信號。如果 AutoResetEvent 處于非終止狀態,則該線程阻塞,并等待當前控制資源的線程,通過調用 Set 發出資源可用的信號。 //調用 Set 向 AutoResetEvent 發信號以釋放等待線程。AutoResetEvent 將保持終止狀態,直到一個正在等待的線程被釋放,然后自動返回非終止狀態。如果沒有任何線程在等待,則狀態將無限期地保持為終止狀態。 //可以通過將一個布爾值傳遞給構造函數來控制 AutoResetEvent 的初始狀態,如果初始狀態為終止狀態,則為 true;否則為 false。 //通俗的來講只有等myResetEven.Set()成功運行后,myResetEven.WaitOne()才能夠獲得運行機會;Set是發信號,WaitOne是等待信號,只有發了信號,等待的才會執行。如果不發的話,WaitOne后面的程序就永遠不會執行。 private static AutoResetEvent autoSet = new AutoResetEvent(false); private static List<Person> list = new List<Person>() { new Person() {Name = "Rose", Age = 19}, new Person() {Name = "Steve", Age = 45}, new Person() {Name = "Jessica", Age = 20}, }; private static void Main(string[] args) { //CheckOrders(); //Common(); //RemoveFromList(); //ExceptionHandling(); //----------------------------------------------------------------- //--------------------------------模擬非線程安全---------------------------- Thread t1 = new Thread(() => { //確保等待t2開始之后才運行下面的代碼 autoSet.WaitOne(); foreach (var item in list) { Console.WriteLine("t1:" + item.Name); Thread.Sleep(1000); } }); t1.Start(); Thread t2 = new Thread(() => { //通知t1可以執行代碼 autoSet.Set(); //沉睡1秒是為了確保刪除操作在t1的迭代過程中 Thread.Sleep(1000); list.RemoveAt(2); }); t2.Start(); Console.ReadKey(); } public static void CheckOrders() { List<Order> orders = new List<Order>() { new Order { Id = 123, Amount = 29.95m, CustomerName = "Mark", Status = "Delivered" }, new Order { Id = 456, Amount = 45.00m, CustomerName = "Steph", Status = "Refunded" }, new Order { Id = 768, Amount = 32.50m, CustomerName = "Claire", Status = "Delivered" }, }; bool anyRefunded = orders.Any(o => o.Status == "Refunded"); if (anyRefunded) { Console.WriteLine("There are refunded orders"); } else { Console.WriteLine("No refunds"); } bool allDelivered = orders.All(o => o.Status == "Delivered"); if (allDelivered) { Console.WriteLine("Everything was delivered"); } else { Console.WriteLine("Not everything was delivered"); } } public static void Common() { //距離圣誕節的天數 var daysToChristmas = (new DateTime(DateTime.Today.Year, 12, 25) - DateTime.Today).TotalDays; Console.WriteLine(daysToChristmas); //----------------------------------------------------------------- int SUM = "10,5,0,8,10,1,4,0,10,1" .Split(',') .Select(int.Parse) .OrderBy(n => n) .Skip(3) .Sum(); Console.WriteLine(SUM); //----------------------------------------------------------------- var customers = new[] { new { Name = "Annie", Email = "annie@test.com" }, new { Name = "Ben", Email = "" }, new { Name = "Lily", Email = "lily@test.com" }, new { Name = "Joel", Email = "joel@test.com" }, new { Name = "Sam", Email = "" }, }; foreach (var customer in from c in customers where !String.IsNullOrEmpty(c.Email) select c) { Console.WriteLine("Sending email to {0}", customer.Name); } //效果同上 foreach (var customer in customers.Where(c => !String.IsNullOrEmpty(c.Email))) { Console.WriteLine("Sending email to {0}", customer.Name); } } public static void RemoveFromList() { Func<List<string>> makeList = () => Enumerable.Range(1, 10000000).Select(n => ("Item " + n + "")).ToList(); var itemsToRemove = new[] { "Item 0", "Item 1", "Item 50", "Item 1000", "Item 999999", "Item 9999999" }; var stopwatch = new Stopwatch(); var list = makeList(); stopwatch.Start(); foreach (var item in itemsToRemove) { list.Remove(item); } stopwatch.Stop(); Console.WriteLine(list.Count + "Foreach took {0}ms", stopwatch.ElapsedMilliseconds); list = makeList(); stopwatch.Restart(); var newList = list.Except(itemsToRemove).ToList(); //效率極低 stopwatch.Stop(); Console.WriteLine(newList.Count + "Except took {0}ms", stopwatch.ElapsedMilliseconds); } public static void ExceptionHandling() { var numbers = Enumerable.Range(1, 10) .Select(n => 5 - n) .Select(n => { try { return 10 / n; } catch (Exception e) { Console.WriteLine("Error in lambda: " + e.Message); return -1; } }); foreach (var n in numbers) { Console.WriteLine(n); } } }
以上是“高效的LINQ語句有哪些”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。