C# Distinct List of Object

今日在Code Review 同事的Code 時候發現了一個有趣的寫法
他想做的是 Return 一個Distinct 左既 List Of Object

好直接既想法是使用 Distinct

List.Distinct();

之後發現Distinct 是不能 Distinct Object的
同事的寫法是用了 List 加 GroupBy 和Select First()

List.GrounBy(x=> x.key).Select(x => x.First())

之後發現.. 原來我們可以使用 GroupBy 加 First 來做到 Distinct Object的效果
以下是我的 Code Sample

void Main()
{
	var list = new List<Example>();
	list.Add(new Example() {Id=1, Name="Example 1", Comment="Comment 1" });
	list.Add(new Example() {Id=1, Name="Example 1", Comment="Comment 1" });
	list.Add(new Example() {Id=1, Name="ExamplE 1", Comment="Comment 2" });
	list.Add(new Example() {Id=2, Name="Example 2", Comment="Comment 3" });
	list.Add(new Example() { Id = 2, Name = "example 2", Comment = "Comment 3" });
	list.Add(new Example() { Id = 3, Name = "Example 3", Comment = "Comment 3" });
	list.Add(new Example() { Id = 4, Name = "Example 4", Comment = "Comment 3" });
	list.Add(new Example() { Id = 5, Name = "Example 5", Comment = "Comment 3" });

	Console.WriteLine("Use List.Distinct()");
	list.Distinct().Dump();

	Console.WriteLine("Use Group By Fields");
	list.GroupBy(x => new {x.Id, Name=x.Name}).Select(x =>x.First()).Dump();

	Console.WriteLine("Use Group By and make it ignore case");
	list.GroupBy(x => new {x.Id, Name=x.Name.ToLower()}).Select(x =>x.First()).Dump();
}

public class Example
{
	public int Id { get; set; }
	public string Name { get; set; }
	public string Comment {get;set;}
}

Output:

Hope you find it useful

EF Core with MySQL Specified key was too long; max key length is 3072 bytes



今日嘗試使用 EF Core with MySQL 時出現了以下的錯誤信息:

<strong>MySql.Data.MySqlClient.MySqlException<br>   HResult=0x80004005<br>   Message=Specified key was too long; max key length is 3072 bytes<br>   Source=MySql.Data<br>   StackTrace:<br>    at MySql.Data.MySqlClient.MySqlStream.ReadPacket()<br>    at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32&amp; affectedRow, Int64&amp; insertedId)<br>    at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force)<br>    at MySql.Data.MySqlClient.MySqlDataReader.NextResult()<br>    at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)<br>    at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()<br>    at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary</strong><code><strong>2 parameterValues)    at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary</strong></code><strong>2 parameterValues)<br>    at Microsoft.EntityFrameworkCore.Migrations.MigrationCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary</strong><code><strong>2 parameterValues)    at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQuery(IEnumerable</strong></code><strong>1 migrationCommands, IRelationalConnection connection)<br>    at Microsoft.EntityFrameworkCore.Storage.RelationalDatabaseCreator.CreateTables()<br>    at Microsoft.EntityFrameworkCore.Storage.RelationalDatabaseCreator.EnsureCreated()<br>    at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.EnsureCreated()<br>    at epm.Api.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, ApplicationDbContext dbContext) in C:\Users\yauch\Documents\git\docker-easy-property\api\src\server\epm.Api\Startup.cs:line 65</strong>

我是使了了Official 的 NuGet PackageMySql.Data.EntityFrameworkCore  “
做了一會research 之後發現… 是這一個package 的問題
解決方法十分簡單.. 我們只需要用另一個 nuget package 便可”Pomelo.EntityFrameworkCore.MySql
https://www.nuget.org/packages/Pomelo.EntityFrameworkCore.MySql

Hope you find it useful



Git Check which branch contain specific commit

今日公司個Production project 有一個 Bug 但係在 Staging 和 UAT environment 都 replicate 唔到呢個問題..
之後便用Git Blame 去檢查一下有什麼 code change
再看看是那一個 commit 做了code change
找到commit 之後
便要嘗試去看看這個commit merge 了去那些 branch
解決方法

我們可以使用以下 git command

git branch --contains $COMMIT_ID

e.g.
git branch --contains 7df12c5

Hope you find it useful

Git Include Ignored files

今日嘗試在git 上加入之前 用 .gitignore Ignore 左既 個project folder入面的 .vscode資料夾
剛剛更新了project 入面的 workspace setting
而在VS Codeworkspace setting 是儲存在 .vscode/settings.json
要把這個檔案加回 source control 我們首先要在 .gitignore 檔案上 移除這個資料夾

之後使用以下 指令 再強制把這個檔案加回source control

git add -f .vscode/settings.json

完成後便可以 commit 這個檔案了
Hope you find it useful