2016/08/26

C#.Net LINQ選擇多重欄位

以下這個範例有兩個結構分別是學生以及分數
兩者相同之處在於結構內都有學生的姓名
所以我們用學生的姓名來判斷是否相同
如果相同則透過new方法來宣告為新的結構以及加入該值


using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;

namespace Test
{
 public partial class Form1 : Form
 {
  /// <summary>
  /// Logger
  /// </summary>
  private static readonly ILog LOG = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType.Name);

  struct Student
  {
   public string name;
  }

  struct Score
  {
   public string name;

   public int math;

   public int ch;

   public int en;
  }

  List<Student> listS = new List<Student>();

  List<Score> listC = new List<Score>();

  public Form1()
  {
   InitializeComponent();
   //初始化Log4Net
   log4net.Config.XmlConfigurator.Configure();

   listS.Add(new Student() { name = "王曉" });
   listS.Add(new Student() { name = "小名" });
   listS.Add(new Student() { name = "鵜鶘" });

   listC.Add(new Score() { name = "王曉", math = 70, ch = 50, en = 100 });
   listC.Add(new Score() { name = "小名", math = 50, ch = 30, en = 1 });


   var listT = from s in listS
      from c in listC
      where s.name.Equals(c.name)
      select new
      {
       s.name,
       c.math,
       c.en,
       c.ch,
       score = (c.math + c.en + c.ch / 3)
      };
   foreach (var t in listT)
   {
    LOG.Info(String.Format("{0} avg:{1}", t.name, t.score));
   }

  }

 }
}


執行結果:

C#.Net Stack Queue

Stack先進後出,玩樸克牌時總是先抽上面的一張
Queue先進先出,去超商時服務人員會依照順序結帳


using log4net;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Windows.Forms;

namespace Test
{
 public partial class Form1 : Form
 {
  /// <summary>
  /// Logger
  /// </summary>
  private static readonly ILog LOG = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType.Name);

  private Stack<String> stack = new Stack<string>();

  private Queue<String> queue = new Queue<string>();

  private readonly String[] ARRAY = new String[] { "王小名", "白獅獅", "大鯨魚" };

  public Form1()
  {
   InitializeComponent();
   //初始化Log4Net
   log4net.Config.XmlConfigurator.Configure();

   var index = 0;
   foreach (var a in ARRAY)
   {
    stack.Push(a);
    queue.Enqueue(a);
    LOG.Info(String.Format("第{0}個加入{1}", ++index, a));
   }

   index = 0;
   LOG.Info("Stack");
   while (stack.Count > 0)
   {
    LOG.Info(String.Format("第{0}個移出{1}", ++index, stack.Pop()));
   }

   index = 0;
   LOG.Info("Queue");
   while (queue.Count > 0)
   {
    LOG.Info(String.Format("第{0}個移出{1}", ++index, queue.Dequeue()));
   }
  }

 }
}

2016/08/23

Windows Form Log4Net

Log4Net是一個可以紀錄運作和系統狀態以及錯誤訊息的Library,可用於.Net平台
安裝則可以透過Nuget輸入
Install-Package log4net

該範例則提供固定的log檔以及依照日期下去產生的log檔,使用前需要至App.config設定並在程式內調用Log4Net的方法

using log4net;
using System;
using System.Reflection;
using System.Windows.Forms;

namespace Test
{
  public partial class Form1 : Form
  {
    /// <summary>
    /// Logger
    /// </summary>
    private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType.Name);

    public Form1()
    {
      InitializeComponent();
      //初始化Log4Net
      log4net.Config.XmlConfigurator.Configure();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      //輸出至log檔
      log.Info("button1Click");

      try
      {
        Convert.ToDouble("ss");
      }
      catch (FormatException ex)
      {
        //將錯誤訊息輸出至log檔
        log.Error(ex.StackTrace);
      }
    }

  }
}




2016/08/22

Git ssh: connect to host bitbucket.org port 22: Connection timed out

今天要上傳程式碼到Bitbucket上沒想到出現了ssh: connect to host bitbucket.org port 22: Connection timed out
解決方式有三種

#1
#改用Https協定
#編輯git設定檔
git config --local -e

#將url修改為https
#url = https://UserName@bitbucket.org/UserName/repository.git

#2
#將22Port改為443
vi ~/.ssh/config

#將下列貼到config內
Host bitbucket.org
  Hostname ssh.bitbucket.org
  Port 443

#用下列指令測試是否連線正確
ssh -T -p 443 git@bitbucket.org

#3
#打給你的ISP請他重新分配一組IP給你

2016/08/20

NuGet 基本操作



#安裝某一套件
Install-Package PackageID

#更新全部套件
Update-Package

#更新某一套件
Update-Package PackageID

#重新安裝全部套件
Update-Package -Reinstall

#移除某一套件
Uninstall-Package PackageID

#取得目前安裝套件
Get-Package