[ASP.NET] LINQ 牛刀小試

[ASP.NET] LINQ 牛刀小試

先感謝點部落上次的贈書活動,贈了LINQ的書,

再看的過程中我想到以前遇到的一個考題,

有ABCD四台Server分別給他們的連線速度和是否可連接,

要如何去判斷哪台可以連接而且速度最快?(不能使用資料庫)

A可連線,連線速度為15kbps,B可連線,連線速度為30kbps,

C不可連線,連線速度為40kbps,D可連線,連線速度為10kbps,

然後有兩個函數一個是get_speed(Server Name)可取得Server的速度

connect(Server Name)可取得Server是否可連接,

我之前的方法是用二維陣列來存,先判斷Server是否連接,

然後用Bubble Sort來排序由大到小取第一個(因為寫Bubble Sort比較簡單),

寫了一堆才解決這個問題,可是用LINQ卻可以輕鬆解決(寫不好請多包涵),

首先寫一個簡單的物件,有Server名稱、Server速度和Server可否連接這些屬性,

然後把get_speed()和connect()寫成物件的方法,


public  class objServer
{
     private object _ServerName;
     private object _ServerSpeed;
     private object _ServerConnect;

     public  object ServerName
    {
        get
        {
            return _ServerName;
        }
        set
        {
            _ServerName = value;
        }
    }
      public  object ServerSpeed
    {
        get
        {
            return _ServerSpeed;
        }
        set
        {
            _ServerSpeed = value;
        }
    }

      public object ServerConnect
    {
        get
        {
            return _ServerConnect;
        }
        set
        {
            _ServerConnect = value;
        }
    }
      public int get_speed()
      {
          return Convert.ToInt16(ServerSpeed);
      }

      public bool connect()
      {
          return Convert.ToBoolean(ServerConnect);
      }
}

接下來建立ABCD這4台Server,然後把這四個物件丟到List裡面,

把物件放入List或是Array不是什麼新鮮事,

但是用物件的屬性或方法來對物件排序,就真的蠻酷的,


using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Text;
using System.Web.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Linq;

public partial class exam4 : System.Web.UI.Page
{
    private objServer _A;
    private objServer _B;
    private objServer _C;
    private objServer _D;

    public  objServer A
    {
        get
        {
            _A = new objServer();
            _A.ServerName = "A";
            _A.ServerSpeed = 15;
            _A.ServerConnect = true;
            return _A;
        }
       
    }
    public  objServer B
    {
        get
        {
            _B = new objServer();
            _B.ServerName = "B";
            _B.ServerSpeed = 30;
            _B.ServerConnect = true;
            return _B;
        }
     
    }
    public  objServer C
    {
        get
        {
            _C = new objServer();
            _C.ServerName = "C";
            _C.ServerSpeed = 40;
            _C.ServerConnect = false;
            return _C;
        }
     
    }
    public  objServer D
    {
        get
        {
            _D = new objServer();
            _D.ServerName = "D";
            _D.ServerSpeed = 10;
            _D.ServerConnect = true;
            return _D;
        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        List Server = new List { A, B, C, D };
        IEnumerable enumServer =
            from myServer in Server
            where myServer.connect() == true
            orderby myServer.get_speed() descending
            select myServer;
        objServer CorrectServer = enumServer.First();

        this.Response.Write("ServerName:"+ CorrectServer.ServerName.ToString() + ", Speed:" + CorrectServer.ServerSpeed.ToString() + "Kbps");
      
    }
}

像我是用IEnumerable來實作在Where的條件式用myServer.connect() == true來篩選可連接的,

然後再orderby來排序由大到小,依造物件的方法來篩選和排序物件,真的很方便

最後再選出已經篩選和排序好的第一個物件,就像是SQL裡面的Top 1,

這樣就可以輕鬆的解決這個問題,END…