mysql> show variables like 'character_set_server'; +----------------------+---------+ | Variable_name | Value | +----------------------+---------+ | character_set_server | utf8mb4 | +----------------------+---------+ 1 row in set (0.06 sec)
mysql> show variables like 'collation_server'; +------------------+--------------------+ | Variable_name | Value | +------------------+--------------------+ | collation_server | utf8mb4_general_ci | +------------------+--------------------+ 1 row in set (0.05 sec)
mysql> show variables like 'character_set_database'; +------------------------+---------+ | Variable_name | Value | +------------------------+---------+ | character_set_database | utf8mb4 | +------------------------+---------+ 1 row in set (0.07 sec)
mysql> show variables like 'collation_database'; +--------------------+--------------------+ | Variable_name | Value | +--------------------+--------------------+ | collation_database | utf8mb4_general_ci | +--------------------+--------------------+ 1 row in set (0.09 sec)
mysql> show variables like 'character_set_database'; +------------------------+---------+ | Variable_name | Value | +------------------------+---------+ | character_set_database | utf8mb4 | +------------------------+---------+ 1 row in set (0.09 sec)
修改数据库的字符集还有排序规则的方式:
1 2 3 4 5 6 7 8 9 10 11
mysql> alter database test_db character set = 'utf8'; Query OK, 1 row affected (0.01 sec)
mysql> show variables like 'character_set_database'; +------------------------+-------+ | Variable_name | Value | +------------------------+-------+ | character_set_database | utf8 | +------------------------+-------+ 1 row in set (0.08 sec)
mysql> create table test (name varchar(32) character set utf8 collate utf8_bin) character set utf8mb4 collate utf8mb4_bin; Query OK, 0 rows affected (0.03 sec)
mysql> show create table test; +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------+ | test | CREATE TABLE `test` ( `name` varchar(32) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin | +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.09 sec)
mysql> alter table test modify column name varchar(32) COLLATE latin1_bin; Query OK, 0 rows affected (0.09 sec) Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table test; +-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------+ | test | CREATE TABLE `test` ( `name` varchar(32) CHARACTER SET latin1 COLLATE latin1_bin DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin | +-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.09 sec)
由于操作具体列数据的时候需要编码转换,如果character_set_connection和字段一致的话,就不用转换了,所以设置character_set_connection可以让 MySQL 用一种编码理解命令统一处理,同时设置character_set_connection为最常用的可以减少转换。
publicstaticIEnumerable<TSource> Distinct<TSource, TKey>( this IEnumerable<TSource> source, Func<TSource, TKey> keySelector) { var hashSet = new HashSet<TKey>(); foreach (TSource element in source) { if (hashSet.Add(keySelector(element))) { yieldreturn element; } } }
最后调用上述扩展方法即可去重,如下:
1
list = list.Distinct(d => new { d.Age, d.Name }).ToList();
publicstaticclassExtensions { publicstaticIEnumerable<T> Distinct<T>( this IEnumerable<T> source, Func<T, T, bool> comparer) where T : class => source.Distinct(new DynamicEqualityComparer<T>(comparer));