GenericQuery

List<Store> StoreList = new List<Store>
{
new Store { WareHouse="一号仓库", Product="电视机", Quantity=10 },
new Store { WareHouse="一号仓库", Product="电视机", Quantity=20 },
new Store { WareHouse="一号仓库", Product="洗衣机", Quantity=30 },
new Store { WareHouse="二号仓库", Product="洗衣机", Quantity=40 }
};

var query = ConditionQuery<Store>(StoreList, itm => itm.WareHouse == "一号仓库");
GridView1.DataSource = query;
GridView1.DataBind();
//var query2 = ConditionQuery<Store>(StoreList, itm => itm.Product == "洗衣机");
//GridView1.DataSource = query2;
//GridView1.DataBind();

//List<int> ints = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
//var qry = ConditionQuery<int>(ints, itm => itm % 2 == 0);
//foreach (var item in qry)
//{
// Response.Write(item.ToString() + ",");
//}

 

public IEnumerable<TSource> ConditionQuery<TSource>(IEnumerable<TSource> source, Func<TSource, bool> condition)
{
          return source.Where(condition);
}

class Store
{
public string WareHouse { get; set; }
public string Product { get; set; }
public int Quantity { get; set; }
}

posted @ 2013-01-19 11:48  yellowshorts  阅读(161)  评论(0编辑  收藏  举报