替代如果,否则如果 [英] Alternative to if, else if

查看:170
本文介绍了替代如果,否则如果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多的如果,else if语句,我知道必须有一个更好的方式来做到这一点,但即使在搜索计算器我不确定如何在我的特定情况下这样做的。

I have a lot of if, else if statements and I know there has to be a better way to do this but even after searching stackoverflow I'm unsure of how to do so in my particular case.

我解析文本文件(票据),并指定服务供应商的名称基于如果某些字符串出现在该法案的变量(txtvar.Provider)。

I am parsing text files (bills) and assigning the name of the service provider to a variable (txtvar.Provider) based on if certain strings appear on the bill.

这是我在做什么(不要笑,我知道这是混乱的)一个小样本。总而言之,目前大约有300如果,如果的事情。

This is a small sample of what I'm doing (don't laugh, I know it's messy). All in all, There are approximately 300 if, else if's.

if (txtvar.BillText.IndexOf("SWGAS.COM") > -1)
{
    txtvar.Provider = "Southwest Gas";
}
else if (txtvar.BillText.IndexOf("georgiapower.com") > -1)
{
    txtvar.Provider = "Georgia Power";
}
else if (txtvar.BillText.IndexOf("City of Austin") > -1)
{
    txtvar.Provider = "City of Austin";
}
// And so forth for many different strings



我想使用像switch语句更有效率和可读性,但我不确定我会如何比较BillText。我正在寻找这样的事情,但不能弄清楚如何使它发挥作用。

I would like to use something like a switch statement to be more efficient and readable but I'm unsure of how I would compare the BillText. I'm looking for something like this but can't figure out how to make it work.

switch (txtvar.BillText)
{
    case txtvar.BillText.IndexOf("Southwest Gas") > -1:
        txtvar.Provider = "Southwest Gas";
        break;
    case txtvar.BillText.IndexOf("TexasGas.com") > -1:
        txtvar.Provider = "Texas Gas";
        break;
    case txtvar.BillText.IndexOf("Southern") > -1:
        txtvar.Provider = "Southern Power & Gas";
        break;
}



我绝对开放的想法。

I'm definitely open to ideas.

编辑::要回答这个问题被认为......是的,我需要确定该值计算顺序的能力的问题。
你可以想像,解析数百稍有不同的布局,当我偶尔碰上作为该法案属于什么样的服务提供商不具有明显的独特指标的问题。
(感谢所有的真棒建议!我已经离开办公室几天,并得到周围尝试出来ASAP)

To answer the question that is being assumed... Yes, I would need the ability to determine the order in which the values were evaluated. As you can imagine, when parsing for hundreds of slightly different layouts I occasionally run into the issue of not having a distinctly unique indicator as to what service provider the bill belongs to. (Thanks for all the awesome suggestions! I have been out of the office for a few days and will get around to trying them out ASAP)

推荐答案

为什么不使用一切C#所提供的?以下使用匿名类型,集合初始化的隐式类型变量,拉姆达的语法LINQ紧凑,直观,并保持您的修改要求,即模式,以便进行评估:

Why not use everything C# has to offer? The following use of anonymous types, collection initializers, implicitly typed variables, and lambda-syntax LINQ is compact, intuitive, and maintains your modified requirement that patterns be evaluated in order:

var providerMap = new[] {
    new { Pattern = "SWGAS.COM"       , Name = "Southwest Gas" },
    new { Pattern = "georgiapower.com", Name = "Georgia Power" },
    // More specific first
    new { Pattern = "City of Austin"  , Name = "City of Austin" },   
    // Then more general
    new { Pattern = "Austin"          , Name = "Austin Electric Company" }   
    // And for everything else:
    new { Pattern = String.Empty      , Name = "Unknown" }
};

txtVar.Provider = providerMap.First(p => txtVar.BillText.IndexOf(p.Pattern) > -1).Name; 



更可能的是,对图案将来自可配置源,如:

More likely, the pairs of patterns would come from a configurable source, such as:

var providerMap =
    System.IO.File.ReadLines(@"C:\some\folder\providers.psv")
    .Select(line => line.Split('|'))
    .Select(parts => new { Pattern = parts[0], Name = parts[1] }).ToList();



最后,@millimoose指出,匿名类型的方法之间传递时用处不大。在这种情况下,我们可以定义一个繁琐的提供类,并使用对象初始化几乎相同的语法:

Finally, as @millimoose points out, anonymous types are less useful when passed between methods. In that case we can define a trival Provider class and use object initializers for nearly identical syntax:

class Provider { 
    public string Pattern { get; set; } 
    public string Name { get; set; } 
}

var providerMap =
    System.IO.File.ReadLines(@"C:\some\folder\providers.psv")
    .Select(line => line.Split('|'))
    .Select(parts => new Provider() { Pattern = parts[0], Name = parts[1] }).ToList();

这篇关于替代如果,否则如果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆