2018-05-13 00:37

C# 從 Delegate 到 Lambda

  1. /* 定義一個 delegate */ 
  2. public delegate string MyDel(int id); 
  3.  
  4.  
  5. /* 宣告一個 Method */ 
  6. public static string MethodA(int id) 
  7. { 
  8.    return " { " + id + " } "; 
  9. } 
  10.  
  11.  
  12. /* 將 MethodA 指派給一個 MyDel */ 
  13. MyDel fu = MethodA; 
  14.  
  15. /* 接著就可以這樣呼叫 delegate */ 
  16. string result = fu(22); 
  17. System.Console.WriteLine(result); 
  18.  
  19.  
  20.  
  21. /* 用 delegate 關鍵字建立 MyDel */ 
  22. MyDel fu = delegate(int id) 
  23. { 
  24.    return " { " + id + " } "; 
  25. }; 
  26.  
  27.  
  28.  
  29. /* 用 Lambda 語法建立 MyDel */ 
  30. MyDel fu = (int id) => { 
  31.    return " { " + id + " } "; 
  32. }; 
  33.  
  34.  
  35.  
  36. /* 因為 MyDel 已經定義參數為 int 了,所以可以省略 int */ 
  37. MyDel fu = (id) => { 
  38.    return " { " + id + " } "; 
  39. }; 
  40.  
  41.  
  42.  
  43. /* 因為是單參數,所以可以省略小刮號 */ 
  44. MyDel fu = id => { 
  45.    return " { " + id + " } "; 
  46. }; 
  47.  
  48.  
  49.  
  50. /* 接著是單純的 return,所以可以省略大刮號跟 return */ 
  51. MyDel fu = id => " { " + id + " } "; 
  52.  
  53.   


Ref:
匿名函式 (C# 程式設計手冊)
使用委派 (C# 程式設計手冊)
2018-05-12 23:58

Windows Server 磁碟清理


磁碟清理
DISM.exe /online /Cleanup-Image /StartComponentCleanup

清理系統 Service Pack 備份檔案
DISM.exe /Online /Cleanup-Image /SPSuperseded
2018-05-12 23:56

JavaScript 正規表示式 跳脫

  1. RegExp.escape = function(str) { 
  2.    return str.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&'); 
  3. }; 
  4.  
  5. /* use sample */ 
  6. new RegExp(RegExp.escape("[te()st]")); 
2018-05-12 23:53

Visual Studio 快捷鍵

Ctrl + L刪除行
Ctrl + X剪下行(游標不選取文字)
Ctrl + C複製行(游標不選取文字)
Ctrl + J呼叫出類別成員
Ctrl + K,C註解選取範圍
Ctrl + K,U取消註解選取範圍
Ctrl + R,R重新命名變數
F2重新命名變數
Ctrl + Enter上方插入一列
Ctrl + Shift + Enter下方插入一列
Ctrl + 减號回到上次游標位置
CTRL + SHIFT + 减號反之
Ctrl + F3找當前選取
F3找下一個
Shift + F3找上一個
Ctrl + F尋找文字
Ctrl + Shift + F跨檔案尋找文字
Ctrl + H取代文字
Ctrl + Shift + H跨檔案取代文字
Ctrl + J顯示物件的成員清單
Ctrl + K, D格式化文件
Ctrl + K, F格式化選取範圍
Ctrl + E, S顯示空白字元
Ctrl + E, \刪除行尾空白
Ctrl + Alt + ]對齊等號
Ctrl + ]切換至對應刮號
F12轉至定義
Ctrl + F12轉至實做
Alt + F12查看定義
F5偵錯建置
Ctrl + F5建置
Ctrl + U小寫
Ctrl + Shift + U大寫
Alt + 上選取行上移
Alt + 下選取行下移
Ctrl + K, K切換書籤
Ctrl + .開啟智慧標籤選單
Shift + Alt + F10開啟智慧標籤選單
2018-04-16 11:08

[VBA] Read Write UTF-8 File

  1. Dim reader As Object 
  2. Set reader = CreateObject("ADODB.Stream") 
  3. reader.CharSet = "UTF-8" 
  4. reader.LineSeparator = 10 
  5. reader.Open 
  6. reader.LoadFromFile "C:\test.txt" 
  7.  
  8. Dim textLine As String 
  9. Do Until reader.EOS 
  10.    textLine = reader.ReadText(-2) 
  11. Loop 
  12. reader.Close 


  1. Dim writer As Object 
  2. Set writer = CreateObject("ADODB.Stream") 
  3. writer.CharSet = "UTF-8" 
  4. writer.Open 
  5. writer.WriteText "xxxxxxx" & Chr(10) 
  6.  
  7. writer.SaveToFile "C:\test.txt", 2 
  8. writer.Close 
2018-04-13 10:59

擴充 LinqToSql 的 Left Join 關連 Table

Linq 在處理 Left Join 的語法有點煩人,想採用 EntityRef 的方式進行查詢,而建立 Entity 的關連表的方式很多,但 DBML 都是透過 Visual Studio 的工具去維護的,手動去修改他實在是不優啊!利用 partial class 的方式就可以避開這種問題。

在 AssociationAttribute 中的 IsForeignKey = false 就會採用 Left Join 進行關連查詢。

  1. using System.Data.Linq; 
  2. using System.Data.Linq.Mapping; 
  3.  
  4. namespace MyConsoleApp.Database 
  5. { 
  6.    public partial class JobCommandMaterial 
  7.    { 
  8.        private Link<Material> _Material; 
  9.  
  10.        [Association(Storage = "_Material", ThisKey = "MaterialCode", OtherKey = "MaterialCode")] 
  11.        public Material Material { get { return _Material.Value; } } 
  12.    } 
  13. } 

參考:
AssociationAttribute 類別 (System.Data.Linq.Mapping)
EntityRef(TEntity) 結構 (System.Data.Linq)
Link(T) 結構 (System.Data.Linq)
2018-04-11 14:07

LinqToSql 替換查詢的 Table

因為封存的 Table 是基於 Base Table 複製出來的,DBML 也不可能一開始就建立這些封存的 Table,但又想用 LinqToSql 去處理資料存取,所以想到用程式去換掉 SQL 裡的 Table Name。

  1. var query = _dc.CtrlCommandRecord.Where(x => x.CreateDate > DateTime.Today); 
  2.  
  3. DbCommand command = _dc.GetCommand(query); 
  4. command.CommandText = command.CommandText.Replace("CtrlCommandRecord", "CtrlCommandRecord_2017"); 
  5. command.Connection = _dc.Connection; 
  6. command.Connection.Open(); 
  7.  
  8. DbDataReader reader = command.ExecuteReader(System.Data.CommandBehavior.CloseConnection); 
  9. var list =  _dc.Translate<CtrlCommandRecord>(reader); 
  10.  
  11. list.Dump(); 
2018-04-11 12:30

用 Visual Studio 自動壓縮發佈的程式

針對只能用隨身碟安裝程式,又不想用 Web Deploy 這麼複雜工具,簡單的用壓縮檔來是處理,利用 Visual Studio 的檔案發佈再進行壓縮封裝,在原本的 pubxml 檔的後面加上 ZipPublishOutput Target 來接續發佈處理,這樣在 Visual Studio 在執行發佈時就可以產生壓縮封裝。

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <!-- 
  3. "C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe" MySolution.sln /t:Rebuild /p:VisualStudioVersion=14.0;Configuration=Release;DeployOnBuild=true;PublishProfile=Release.pubxml 
  4. --> 
  5. <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 
  6.  <PropertyGroup> 
  7.    <WebPublishMethod>FileSystem</WebPublishMethod> 
  8.    <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration> 
  9.    <LastUsedPlatform>Any CPU</LastUsedPlatform> 
  10.    <SiteUrlToLaunchAfterPublish /> 
  11.    <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish> 
  12.    <ExcludeApp_Data>True</ExcludeApp_Data> 
  13.    <publishUrl>obj\Publish</publishUrl> 
  14.    <DeleteExistingFiles>True</DeleteExistingFiles> 
  15.    <AutoParameterizationWebConfigConnectionStrings>False</AutoParameterizationWebConfigConnectionStrings> 
  16.  </PropertyGroup> 
  17.  
  18.  <Target Name="ZipPublishOutput" AfterTargets="GatherAllFilesToPublish;WebFileSystemPublish"> 
  19.    <Message Text="== 封裝 7-Zip 安裝包 ===============================" Importance="high" /> 
  20.    <PropertyGroup> 
  21.      <Today>$([System.DateTime]::Today.ToString("yyyyMMdd"))</Today> 
  22.      <zip>"C:\Program Files\7-Zip\7z.exe"</zip> 
  23.      <ReleasePath>$(ProjectDir)obj\Release\Package\PackageTmp</ReleasePath> 
  24.      <PackagePath>D:\MySolution_$(Today).exe</PackagePath> 
  25.    </PropertyGroup> 
  26.  
  27.    <Exec Command='DEL $(PackagePath)' /> 
  28.    <Exec Command='$(zip) a -t7z -mx9 -sfx7z.sfx -bd $(PackagePath) * | findstr /v "^$"' WorkingDirectory="$(ReleasePath)" /> 
  29.  </Target> 
  30. </Project>