Sort
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Linq
{
public class Product
{
private string name;
private int price;
public Product(int _price,string _name)
{
Name = _name;
Price = _price;
}
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public int Price
{
get
{
return price;
}
set
{
price = value;
}
}
}
class Program
{
static void Main(string[] args)
{
List<Product> product = new List<Product>();
product.Add(new Product(10, "name1"));
product.Add(new Product(500, "name2"));
product.Add(new Product(72, "name5"));
product.Add(new Product(9, "name4"));
//List<Product> cheapest = product.FindAll(delegate(Product p){ return p.Price < 25 || p.Name.Contains("5"); });
// 因为sort 方法是 void 类型 所以 直接执行后 product发生改变
// 降序:
//lstroot.Sort((x, y) => y.static_count.CompareTo(x.static_count));
// 升序:
//lstroot.Sort((x, y) => x.static_count.CompareTo(y.static_count));
product.Sort((x, y) => x.Price.CompareTo(y.Price));
foreach (var ss in product)
{
Console.WriteLine(ss.Name+":"+ss.Price);
}
Console.ReadKey();
}
}
}
search for
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Linq
{
public class Product
{
private string name;
private int price;
public Product(int _price,string _name)
{
Name = _name;
Price = _price;
}
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public int Price
{
get
{
return price;
}
set
{
price = value;
}
}
}
class Program
{
static void Main(string[] args)
{
List<Product> product = new List<Product>();
product.Add(new Product(10, "name1"));
product.Add(new Product(500, "name2"));
product.Add(new Product(72, "name5"));
product.Add(new Product(9, "name4"));
//搜索功能
List<Product> cheapest = product.FindAll(delegate (Product p) { return p.Price < 25 || p.Name.Contains("5"); });
//lambda写法
// List<Product> cheapest = product.FindAll(p => p.Price == 500 || p.Name.Contains("5"));
//lambda另一种写法 //List 和 IEnumable要用 tolist转换
// List<Product> cheapest = product.Where(p => p.Price == 500 || p.Name.Contains("5")).ToList();
foreach (var ss in cheapest)
{
Console.WriteLine(ss.Name+":"+ss.Price);
}
Console.ReadKey();
}
}
}