在我目前的工作中,经常发生的事情是有一个通用的过程需要发生,但是该过程的奇数部分需要根据某个变量的值而稍有不同,所以我不是非常确定处理此问题的最优雅方法是什么。
我将使用我们通常使用的示例,该示例在做事时会有所不同,具体取决于所处理的国家/地区。
所以我有一堂课,我们称之为Processor:
public class Processor
{
public string Process(string country, string text)
{
text.Capitalise();
text.RemovePunctuation();
text.Replace("é", "e");
var split = text.Split(",");
string.Join("|", split);
}
}
Run Code Online (Sandbox Code Playgroud)
除了某些国家只需要采取其中一些行动。例如,只有6个国家需要资本化步骤。要分割的字符可能会因国家/地区而异。'e'仅根据国家/地区,才需要替换重音符号。
显然,您可以通过执行以下操作来解决此问题:
public string Process(string country, string text)
{
if (country == "USA" || country == "GBR")
{
text.Capitalise();
}
if (country == "DEU")
{
text.RemovePunctuation();
}
if (country != "FRA")
{
text.Replace("é", "e");
}
var separator = DetermineSeparator(country);
var split = text.Split(separator);
string.Join("|", split);
}
Run Code Online (Sandbox Code Playgroud)
但是,当您与世界上所有可能的国家打交道时,这将变得非常麻烦。而且无论如何,这些if语句使逻辑更难以阅读(至少,如果您想象的是比示例更复杂的方法),则循环复杂性开始迅速攀升。
所以目前我正在做这样的事情: …
在 VS 2017 Enterprise 上复制的步骤:
没有“抑制”选项可用。
有谁知道为什么没有抑制警告?
c# code-analysis visual-studio .net-core roslyn-code-analysis
(正在使用SQL Server 2012)
我找到了一些关于查询优化的主题,并将EXISTS与COUNT进行比较,但我找不到这个确切的问题.
我有一个看起来像这样的查询:
select * from
tblAccount as acc
join tblUser as user on acc.AccountId = user.AccountId
join tblAddress as addr on acc.AccountId = addr.AccountId
... **a few more joins**
where acc.AccountId in (
select * accountid from
(select accountid, count(*) from tblUser
where flag = 1
group by accountId) as tbl where c != 1
Run Code Online (Sandbox Code Playgroud)
此查询立即运行(尽管db非常大,大约70Gb).
当我将查询包装在EXISTS中时,如下所示:
if exists
(
**Exact same query as above**
)
begin
RAISERROR('Account found without exactly one flagged user.', 16, 1);
end …Run Code Online (Sandbox Code Playgroud)