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; } }Program.cs代码如下:
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语句代码Model层的类如下:Program.cs代码如下:全部内容,希望文章能够帮你解决分享一些高效的LINQ语句代码Model层的类如下:Program.cs代码如下:所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)