LinQ 是.NET 3.5 在数据处理方面的一个新特性, 又可以分为 linq to sql || linq to xml || linq to object 等等,下面介绍 linq to array 和 linq to list!
linq 基本语法 : from 数据实体 in 数据实体容器 where 条件 group by 排序 select 数据实体
linq 投影语法 : from 数据实体 in 数据实体容器 where 条件 group by 排序 select new{ParamName=数据实体.属性 , ..,..} //注意: 可以有多个自定义属性,中间用逗号隔开,在数据绑定的时候,只能帮顶新声明的数据名称
linq 还可以使用匿名委托进行查询过滤 下面是各种实例:
public class City
{
string name;
int price;
public string Name
{
set { name = value; }
get { return name; }
}
public int Price
{
set { price = value; }
get { return price; }
}
public static List<City> GetList
{
get {
return new List<City>() {
new City(){Name="Jg",Price=6},
new City(){Name="Zl",Price=5},
new City(){Name="Xj",Price=8}
};
}
}
}
public void Bind()
{
//方法1
//var result = from ci in City.GetList select new {Price = ci.Price+1 };
//dlstCity.DataSource = result;
//dlstCity.DataTextField = "Price";
//dlstCity.DataBind();
//方法2
//List<City> citys = City.GetList;
//var result = citys.Where(city=>city.Price > 5);
//dlstCity.DataSource = result;
//dlstCity.DataTextField = "Price";
//dlstCity.DataBind();
//方法3
List<City> citys = City.GetList;
var result = citys.Where(delegate(City city) { return city.Price > 5; });
dlstCity.DataSource = result;
dlstCity.DataTextField = "Price";
dlstCity.DataBind();
}
string[] citys = {"青岛","北京","上海","深圳","香港"};
var city = from c in citys select c;
dlstCity.DataSource = city;
dlstCity.DataBind();
转载自:http://hi.baidu.com/duogemajia/blog/item/047257a338643ba4caefd004.html