1 Lambda表达式初步
1.1 A
delegate string ProcessStr(string input);
protected void Page_Load(object sender, EventArgs e)
{
var foxRiver = new string[] { "Mike", "Smith", "Tom", "Jack" };
ProcessStr p = input => input.ToLower();
foreach (string name in foxRiver)
{
Response.Write(p(name));
}
1.2
List<string> foxRiver = new List<string>{ "Mike", "Smith", "Tom", "Jack" };
string val1 = foxRiver.Find(i => i.IndexOf("J")==0);
Response.Write(val1);
1.3 Select和Where的用法
List<string> foxRiver = new List<string>{ "Mike", "Smith", "Tom", "Jack","Mark","Medy" };
var foxs=foxRiver.Where(p => p.StartsWith("M")).Select(p => p.ToLower());
foreach (var a in foxs)
{
Response.Write(a+" ");
}
1.4
delegate string ProcessStr(string input);
protected void Page_Load(object sender, EventArgs e)
{
List<Person> lst = new List<Person>{
new Person{ Name="Mike", Age=23, Address=new Address{ Country="China", City="gz"}},
new Person{ Name="Jack", Age=30, Address=new Address{ Country="China", City="sh"}},
new Person{ Name="Rose", Age=25, Address=new Address{ Country="USA", City="gz"}},
new Person{ Name="Tome", Age=24, Address=new Address{ Country="China", City="gz"}},
new Person{ Name="Andy", Age=23, Address=new Address{ Country="USA", City="gz"}},
new Person{ Name="Mark", Age=28, Address=new Address{ Country="China", City="gz"}},
new Person{ Name="Smith", Age=20, Address=new Address{ Country="Korea", City="gz"}},
new Person{ Name="Medy", Age=30, Address=new Address{ Country="Korea", City="gz"}}, };
var newPs = from p in lst group p by p.Address.Country into newP select newP;
foreach (var p in newPs)
{
Response.Write(p.First().Address.Country+" "+p.Average(p1 => p1.Age)+"<br>");
}
1.5
2
int[] scores = new int[] { 2, 5, 8, 3, 6, 1, 9 };
var event1=scores.Where(p => p % 2 == 0).Select(p => p).OrderByDescending(p => p);
foreach (int a in event1)
{
Response.Write(a.ToString());
}
3
string[] cities = { "London", "Amsterdam", "San Francisco", "Las Vegas","Boston", "Raleigh", "Chicago",
"Charlestown", "Helsinki", "Nice", "Dublin" };
GridView1.DataSource = from city in cities where city.Length > 4 orderby city select city.ToUpper();
GridView1.DataBind();
4 搜索一个数组的字符串并没多大意思,虽然有时候很有用。如果我们能对自己的功能更丰富的那些集合中搜索将 会更有趣。好消息是,LINQ使这些变得很简单。例如,为了更好记录我去过的地方,我在我的工程中建立了一 个叫"Location"的简单类:
<asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:BoundField HeaderText="Country" DataField="Country" />
<asp:BoundField HeaderText="City" DataField="City" />
<asp:BoundField HeaderText="Distance from Seattle" DataField="Distance" />
</Columns>
</asp:GridView>
List<Location> cities = new List<Location>{
new Location { City="London", Distance=4789, Country="UK" },
new Location { City="Amsterdam", Distance=4869, Country="Netherlands" },
new Location { City="San Francisco", Distance=684, Country="USA" },
new Location { City="Las Vegas", Distance=872, Country="USA" },
new Location { City="Boston", Distance=2488, Country="USA" },
new Location { City="Raleigh", Distance=2363, Country="USA" },
new Location { City="Chicago", Distance=1733, Country="USA" },
new Location { City="Charleston", Distance=2421, Country="USA" },
new Location { City="Helsinki", Distance=4771, Country="Finland" },
new Location { City="Nice", Distance=5428, Country="France" },
new Location { City="Dublin", Distance=4527, Country="Ireland" }
};
GridView1.DataSource = from location in cities
where location.Distance > 1000
orderby location.Country, location.City
select location;
GridView1.DataBind();
5 稍微重构一下City集合
因为我们将在好几个示例中重用这个城市集合,我决定把它封装到一个"TravelOrganizer"类中
public class TravelOrganizer
{
public List<Location> PlacesVisited
{
get
{
List<Location> cities = new List<Location>{
new Location { City="London", Distance=4789, Country="UK" },
new Location { City="Amsterdam", Distance=4869, Country="Netherlands" },
new Location { City="San Francisco", Distance=684, Country="USA" },
new Location { City="Las Vegas", Distance=872, Country="USA" },
new Location { City="Boston", Distance=2488, Country="USA" },
new Location { City="Raleigh", Distance=2363, Country="USA" },
new Location { City="Chicago", Distance=1733, Country="USA" },
new Location { City="Charleston", Distance=2421, Country="USA" },
new Location { City="Helsinki", Distance=4771, Country="Finland" },
new Location { City="Nice", Distance=5428, Country="France" },
new Location { City="Dublin", Distance=4527, Country="Ireland" }
};
}
}
}
TravelOrganizer travel = new TravelOrganizer();
GridView1.DataSource = from location in travel.PlacesVisited
where location.Distance > 1000
orderby location.Country, location.City
select location;
GridView1.DataBind();