• LINQ to Object的一个例子


    查询两个集合中匹配数据并输出, 能写得这样容易真是让人有点震撼, 不学习真是要落伍了!

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace ConsoleApplication4
    {
        class Program
        {
            static void Main(string[] args)
            {
                List<Product> list = new List<Product>();
                list.Add(new Product("htc", 3000));
                list.Add(new Product("nokia", 2000));
                list.Add(new Product("moto", 1000));


                List<Company> company_list = new List<Company>();
                company_list.Add(new Company("htc","taiwan"));
                company_list.Add(new Company("nokia","swiden"));
                company_list.Add(new Company("moto","america"));

                var fliter = from Product p in list
                             join Company c1 in company_list on p.Name equals c1.CompanyName
                             where p.Price > 1000
                             select new { p.Name, p.Price, c1.CompanyAddress };

                foreach (var v in fliter)
                {
                    Console.WriteLine(v.Name + v.CompanyAddress + v.Price );
                }

                Console.ReadKey();
            }
        }


        public class Product
        {
            public string Name { set; get; }
            public decimal Price { set; get; }

            public Product(string Name, decimal Price)
            {
                this.Name = Name;
                this.Price = Price;
            }
        }

        public class Company
        {
            public string CompanyName { set; get; }
            public string CompanyAddress { set; get; }

            public Company(string _Name, string _Address)
            {
                this.CompanyName = _Name;
                this.CompanyAddress = _Address;
            }
        }
    }

    ************************************************************

    这段集合初始化还是落伍了.

                List<Product> list = new List<Product>();
                list.Add(new Product("htc", 3000));
                list.Add(new Product("nokia", 2000));
                list.Add(new Product("moto", 1000));


                List<Company> company_list = new List<Company>();
                company_list.Add(new Company("htc","taiwan"));
                company_list.Add(new Company("nokia","swiden"));
                company_list.Add(new Company("moto","america"));

    可以改造如下:

                List<Product> list = new List<Product>(){
                  new Product("htc", 3000),
                  new Product("nokia", 2000),
                  new Product("moto",1000)           
                };


                List<Company> company_list = new List<Company>()
                {
                   new Company("htc", "taiwan"),
                   new Company("nokia", "swiden"),
                   new Company("moto", "america")
                };

  • 相关阅读:
    Qt编写控件属性设计器12-用户属性
    C#中通过三边长判断三角形类型(三角形测试用例)
    C#中通过Selenium定位<a>标签的问题
    SharePoint自动化系列——Manage "Site Subscriptions" using PowerShell
    SharePoint API测试系列——Records.BypassLocks测试
    SharePoint API测试系列——对Recorded Item做OM操作(委托的妙用)
    放松时刻——C#分割字符串
    链表——PowerShell版
    栈——PowerShell版
    队列——PowerShell版
  • 原文地址:https://www.cnblogs.com/liuzhendong/p/2266275.html
Copyright © 2020-2023  润新知