namespace TestProject1; public static class TenantDbHelper { /// /// Key-Value: {DbName}-{TenantKeyLessThan} /// public static Dictionary DbList { get; set; } = new Dictionary { { "cferp_test_1", 1000 }, { "cferp_test_2", 2000 }, { "cferp_test_3", int.MaxValue }, }; public static string GetDbNameByTenantKeyValue(int tenantKeyValue) { // var dictionary = new SortedDictionary(); // DbList.ForEach(pair => dictionary.Add(pair.Value, pair.Key)); // 注意配置顺序 var dbName = DbList.Cast?>() .FirstOrDefault(pair => pair?.Value != null && pair.Value.Value > tenantKeyValue)!.Value.Key; return dbName ?? throw new ArgumentOutOfRangeException(nameof(tenantKeyValue), $"已配置的数据库中没有任何符合'{nameof(tenantKeyValue)}'值的对象"); } }