查询两个集合中匹配数据并输出, 能写得这样容易真是让人有点震撼, 不学习真是要落伍了!
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")
};