LINQ 基础(二)

本文通过多个实例介绍如何使用LINQ查询语法和Lambda表达式处理数据集合,包括字符串处理、筛选、排序及复杂查询操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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          SelectWhere的用法

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();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值