Share some of the efficient LINQ statement code

Source: Internet
Author: User

The class of the model layer is as follows:

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;}    }

The Program.cs code is as follows:

 Class Program {//autoresetevent allows threads to communicate with each other by signaling.        Typically, this communication involves a resource that the thread requires exclusive access to. The thread waits for a signal by calling WaitOne on AutoResetEvent.        If the AutoResetEvent is in a non-terminating state, the thread blocks and waits for the current control resource's thread to emit a signal that the resource is available by calling Set. Call Set to signal AutoResetEvent to release the waiting thread. AutoResetEvent will remain signaled until a waiting thread is freed and then automatically returned to the non-terminating state.        If no thread is waiting, the state remains in the terminating state indefinitely.        You can control the initial state of the AutoResetEvent by passing a Boolean value to the constructor, true if the initial state is signaled, or false. In layman's terms, only after the successful operation of the Myreseteven.set (), Myreseteven.waitone () will be able to obtain operational opportunities; Set is a signal, WaitOne is waiting for the signal, only send a signal, wait for the execution.        If not, the program behind WaitOne will never be executed.        private static AutoResetEvent AutoSet = new AutoResetEvent (false); private static list<person> List = new list<person> () {new person () {Name = ' Rose ', age = 1        9}, new Person () {name = ' Steve ', age = $}, new person () {name = ' Jessica ', age = 20},};            private static void Main (string[] args) {//checkorders ();            Common ();       RemoveFromList ();     ExceptionHandling (); -----------------------------------------------------------------//--------------------------------Impersonation non-thread safe--                --------------------------Thread T1 = new Thread (() =/= Make sure to wait for T2 to start before running the following code                Autoset.waitone (); foreach (var item in list) {Console.WriteLine ("T1:" + item.                    Name);                Thread.Sleep (1000);            }            }); T1.            Start ();                Thread t2 = new Thread (() + = {//notification T1 can execute code autoset.set ();                Sleeping for 1 seconds is to ensure that the delete operation is Thread.Sleep (1000) during the T1 iteration; 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 = "Step H ", status =" refunded "}, new Order {Id = 768, Amount = 32.50m, CustomerName =" Claire ", status =" delive            Red "},}; BOOL anyrefunded = orders.            any (o = = O.status = = "refunded");            if (anyrefunded) {Console.WriteLine ("There is 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 is delivered"); }} public static void Common () {//Days away from christmas var daystochristmas = (New Datetim E (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). (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 {Na            me = "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); }//Effect as above 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 = () =&G T Enumerable.range (1, 10000000). Select (n = ("Item" + N + "")).            ToList ();            var itemstoremove = new[] {"Item 0", "Item 1", "Item", "Item", "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 (); Very low efficiency 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); }        }    }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.