免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
數(shù)據(jù)訪問技術系列課程(10):.NET Framework 3.5中的LINQ簡介

· LINQ 概覽

原始訪問形式

 SqlConnection conn = new SqlConnection("server=(local);database=AdventureWorks;uid=sa;pwd=password01!");
        SqlCommand comm = new SqlCommand();
        comm.Connection = conn;
        comm.CommandType = CommandType.Text;
        comm.CommandText = "SELECT * FROM HumanResources.Department";

        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(comm);
        da.Fill(ds);

        this.GridView1.DataSource = ds.Tables[0];
        this.GridView1.DataBind();

Linq:

        PubsDataContext dc = new PubsDataContext();
        var result = from item in dc.authors
                     select item;
        this.GridView1.DataSource = result;
        this.GridView1.DataBind();

 

 

 string[] cities = {"Shanghai","Beijing","Dalian",
                               "Chengdu"};

        IEnumerable<string> places = from city in cities
                                     where city.Length > 5
                                     orderby city descending
                                     select city;

        List<string > list = places .ToList<string>();
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        dt.Columns.Add("City", typeof(string));

        for (int i = 0 ;i<list.Count ;i++)
        {
            DataRow dr = dt.NewRow();
            dr["City"] = list[i];
            dt.Rows.Add(dr);
        }
        ds.Tables.Add(dt);

        this.GridView1.DataSource = ds;      
        this.GridView1.DataBind();

 

 

· 訪問數(shù)組

 

 string[] cities = {"Shanghai","Beijing","Dalian",
                               "Chengdu"};

 IEnumerable<string> places = from city in cities
                                     where city.Length > 5
                                     orderby city descending
                                     select city;


        this.GridView1.DataSource = places;      
        this.GridView1.DataBind();

 

· 訪問集合

 

     List<string> list = new List<string>
        {
            "ShangHai", "Beijing","Dalian", "Chengdu","shenyang"
        };


        IEnumerable<string> places = from city in list
                                     where city.Length > 5
                                     orderby city descending
                                     select city;


        this.GridView1.DataSource = places;      
        this.GridView1.DataBind();

 

Demo2:Linq to 集合

 

public class Location
{

    public string Country { get; set; }
    public string City { get; set; }
    public double DistanceFromBeijing { get; set; }

   public Location()
   { 
   }
}

 

code

   List<Location> list = new List<Location>
        {
            new Location {City="Shenyang",Country="China",DistanceFromBeijing=800},
            new Location{City="Shanghai",Country="China",DistanceFromBeijing=1000},
            new Location{City="London",Country ="UK",DistanceFromBeijing=8000}
        };
        IEnumerable<Location> places = from item in list                                    
                                     select item;
        this.GridView1.DataSource = places;      
        this.GridView1.DataBind();

前臺

   <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
            GridLines="None" AutoGenerateColumns="false">           
            <Columns>
                    <asp:BoundField HeaderText="City" DataField="City" />
                     <asp:BoundField HeaderText="Country" DataField="Country" />
                      <asp:BoundField HeaderText="Dist" DataField="DistanceFromBeijing" />
            </Columns>
        </asp:GridView>

 

 

· 查詢投影

·查詢投影(Select)

·不返回所有數(shù)據(jù)列/屬性

·修改或者轉(zhuǎn)化查詢的數(shù)據(jù)

·利用編譯器對"匿名類型"的支持查詢數(shù)據(jù)列/屬性

·生成匿名類型('a')

 

List<City> cities = City.GetLocations();

        var places = from city in cities
                     where city.DistanceFromBeijing > 3000
                     select new
                     {
                         Name = city.Name,
                         Country = city.Country,
                         DistanceFromBeijing = city.DistanceFromBeijing * 1.61
                     };

        GridView1.DataSource = places;
        GridView1.DataBind();

 

 

· 使用lambda表達式

·查詢語法是一個方便的聲明性代碼縮寫,您可以手動編寫它:

·IEnumerable expr = names

                                 .Where(s=>s.Length==5)

                                 .OrderBy(s=>s)

                                 .Select(s=>s.ToUpper());

                                      

 

 

· 查詢操作符

調(diào)用

·普通的方式來調(diào)用擴展方法

   ·IEnumerable<string> query = Enumerable.Where(names,s => s.Length < 6);

· C#語言允許我們使用如下的方式來調(diào)用擴展方法:

    ·IEnumerable<string> query = names.Where(s=> s.Length < 6);

 

 Demo:利用Lambda表達式

        List<Location> list = new List<Location>
        {
            new Location {Name="Shenyang",Country="China",DistanceFromBeijing=800},
            new Location{Name="Shanghai",Country="China",DistanceFromBeijing=1000},
            new Location{Name="London",Country ="UK",DistanceFromBeijing=8000}
        };

        var places = list.Where(city => city.DistanceFromBeijing < 1500)
            .Select(city => new { city.Name, city.Country, DistanceFromBeijing = city.DistanceFromBeijing });


        //IEnumerable<Location> places = from item in list                                    
        //                             select item;
        this.GridView1.DataSource = places;
        this.GridView1.DataBind();

 

------------------

 

Demo:

 

 List<City> list = new List<City>
        {
            new City {Name="Shenyang",Country="China",DistanceFromBeijing=800},
            new City{Name="Shanghai",Country="China",DistanceFromBeijing=1000},
            new City{Name="London",Country ="UK",DistanceFromBeijing=8000}
        };

        //IEnumerable<Location> places = from item in list                                    
        //                             select item;


        //1.
        //var places = list.Where(city => city.DistanceFromBeijing < 1500)
        //    .Select(city => new { city.Name, city.Country, DistanceFromBeijing = city.DistanceFromBeijing });
      
        //2.利用匿名方法       
        //var places = list.Where(delegate(City city) { return (city.DistanceFromBeijing < 1500); })
        //    .Select(city => new { city.Name, city.Country, DistanceFromBeijing = city.DistanceFromBeijing });

 

       //3.

        var places = list.Where(new Func<City, bool>(Predict))
            .Select(city => new { city.Name, city.Country, DistanceFromBeijing = city.DistanceFromBeijing });
               
        this.GridView1.DataSource = places;
        this.GridView1.DataBind();

 

   public bool Predict(City city)
    {
        return city.DistanceFromBeijing < 1500;
    }

 

--------------------------

 

本站僅提供存儲服務,所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權內(nèi)容,請點擊舉報
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
一步一步學Linq to sql(二):DataContext與實體
C#中IEnumerable<T>.Select()、SelectMany()的簡單使用
ASP.NET2.0通過代碼用數(shù)據(jù)適配器對數(shù)據(jù)庫添加、刪除、修改、查詢時的幾個問題
最全的數(shù)據(jù)結(jié)構(gòu)解析與歸納
gridview分頁模型
C#2.0全能數(shù)據(jù)庫組件 (通俗易懂)
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服