日期:2014-05-20  浏览次数:20685 次

关于select、where 基础
刚开始看linq


问题:下面2句,分别用where,select ,有什么区别,查询结果是不是都一样
orders是List<Order> 
var ordByCountry = orders.Where(order => order.ShipCountry.Equals(country));

var ordByCountry = orders.Select(order => order.ShipCountry.Equals(country));


  public class Order
  {
  private int orderID;
  private string customerID;
  private decimal freight;
  private string shipName;
  private string shipAddress;
  private string shipCity;
  private string shipCountry;

  public int OrderID { get; set; }
  public string CustomerID { get; set; }
  public decimal Freight { get; set; }
  public string ShipName { get; set; }
  public string ShipAddress { get; set; }
  public string ShipCity { get; set; }
  public string ShipCountry { get; set; }
  }


------解决方案--------------------
修改一下

var v = list.Select(x => x = 5);返回 5,5,5,5,5,5,5,5,5,5 10个5
------解决方案--------------------
where和select怎么会一样呢

你自己构造点数据,执行下边两个语句就明白了:


var ordByCountry = orders.Where(order => order.ShipCountry.Equals(country));

var ordByCountry1 = orders.Select(order => order.ShipCountry);

------解决方案--------------------
修改下
比如说
C# code
int[] data = { 1, 2, 3, 4, 5, 6 };