有效地比较多个变量到相同的值

时间:2018-05-21 16:30:38

标签: c# conditional

我正在寻找C#中的替代方法来将多个变量与相同的值进行比较,我最好希望他们根据条件结果共享相同的后续指令。

例如,我有以下代码:

string DOBResultsError = VerifySingleRecordReturned(DOBResults, APIParameters, 1);
string NameResultsError = VerifySingleRecordReturned(NameResults, APIParameters, 2);

if (DOBResultsError != string.Empty)
{
    PatientRecordUpdate(DOBResults, APIParameters.PatientID, DOBResultsError);
}
else if (NameResultsError != string.Empty)
{
    PatientRecordUpdate(NameResults, APIParameters.PatientID, NameResultsError);
}

我必须明确指示要对与PatientRecordUpdate进行比较的每个变量执行String.Null

我希望发生的事情如下:

if (DOBResultsError != string.Empty || NameResultsError != string.Empty)
{
    //whichever isn't an empty string use to perform PatientRecordUpdate()
}

C#中是否可以使用这种语法?

使用switch关键字不会产生影响,因为即使我有多种情况导致执行相同的指令,如果我需要使用其中一个比较变量,我仍然需要使用变量为每种可能的情况明确说明代码。

string DOBResultsError = VerifySingleRecordReturned(DOBResults, APIParameters, 1);
string NameResultsError = VerifySingleRecordReturned(NameResults, APIParameters, 2);
string SSNResultsError = VerifySingleRecordReturned(SSNResults, APIParameters, 3);
string EmptyString = String.Empty;

switch (EmptyString)
{
    case DOBResultsError:
    case SSNResultsError: //can't use SSNResultsError with PatientRecordUpdate() without stating PatientRecordUpdate() again
        PatientRecordUpdate(DOBResults, APIParameters.PatientID, DOBResultsError);

    case NameResultsError:
        PatientRecordUpdate(NameResults, APIParameters.PatientID, NameResultsError);
}

任何帮助表示感谢。

更新:请求其他信息

这就是VerifySingleRecordReturnedFrom()的作用。它会检查一些可能导致程序错误的条件,并在SQL DB中的记录中写入一条错误消息。

public static string VerifySingleRecordReturnedFrom(List<PatientList3> ReturnedPatientList, AutoPatientLookup APIParameters, int SearchCriteria = 0)
        {
            string ErrorMessage = String.Empty;
            if (ReturnedPatientList.Count == 0)
            {
                //Error Message for Dob
                if (SearchCriteria == 1)
                {
                    ErrorMessage = string.Format("No patients were returned from  for DOB ....");
                    return ErrorMessage;
                }
                //Error Message for Name
                else if (SearchCriteria == 2)
                {
                    ErrorMessage = string.Format("No patients were returned from  for patient name ....");
                    return ErrorMessage;
                }
                //Error Message for PracticePatientNumber
                else if (SearchCriteria == 3)
                {
                    ErrorMessage = string.Format("No patients were returned from  for PracticePatientNumber...");
                    return ErrorMessage;
                }
            }
            // more than one patient in common results list from AttemptToMatchPatientsByDemographics() or  results using PatientNumber
            else if (ReturnedPatientList.Count() > 1)
            {
                switch(SearchCriteria)
                {
                    case 1:
                    case 2:
                        ErrorMessage = String.Format("{0} number of patients were returned...");
                        break;

                    //More than one patient returned from  for any given PracticePatientNumber
                    case 3:
                        ErrorMessage = String.Format("{0} number of patients were returned....");
                        break;
                }
                return ErrorMessage;
            }
            //No error in number of results from 
            return ErrorMessage;
        }

所有结果(DOB / Name / SSN)类型都是以下PatientList3对象的List对象(我已经包含了子类):

public class PatientList3
        {
            public Patient PatientNameID { get; set; }
            public string PatientNumber { get; set; }
            public string ChartNumber { get; set; }
            public Gender2 Gender { get; set; }
            public string DOB { get; set; }
            public string PhoneNumber { get; set; }
            public string SSN { get; set; }
        }

public class Patient
        {
            public int ID { get; set; }
            public PtName Name { get; set; }
        }

public class PtName
        {
            public string First { get; set; }
            public string Middle { get; set; }
            public string Last { get; set; }
            public string Suffix { get; set; }
            public string Full { get; set; }
            public string Preferred { get; set; }
        }
public class Gender2
        {
            public string LookupType { get; set; }
            public string Code { get; set; }
            public string Description { get; set; }
            public int Order { get; set; }
            public bool Active { get; set; }
            public List<AlternateCodes> AlternateCodes { get; set; } //Not important, didn't include AlternativeCodes class
        }

这是APIParameters的类:

public class AutoPatientLookup
        {
            public string DOB { get; set; }
            public string Gender { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public int? PatientNumber { get; set; }
            public string SSN { get; set; }
            public int PracticeID { get; set; }
            public int PatientID { get; set; }
        }

1 个答案:

答案 0 :(得分:-1)

考虑为某些通用逻辑利用基本结果类型,如下所示:

// assuming both results inherit from or implement some base type...
List<ResultBase> results = new List<ResultBase>()
{
    DOBResults,
    NameResults 
}; // order matters since it's used for the parameter index

// Note that a class would be a much better option than a tuple here.
// Select will verify each result but only upon request until FirstOrDefault is fulfilled
Tuple<ResultBase, string> firstResultError = results
    .Select((result, index) => new Tuple<ResultBase, string>(
        result,
        VerifySingleRecordReturned(result, APIParameters, index + 1)))
    .FirstOrDefault(result => !string.IsNullOrEmpty(result.Item2 /* Error message */));

// If there was at least one error, call the original patient update record
// with the associated information.
if (firstResultError != null)
{
    PatientRecordUpdate(
        firstResultError.Item1 /* Failed result */,
        APIParameters.PatientID,
        firstResultError.Item2 /* Error message for that result */);
}

出于可维护性的原因,您将需要使用新类而不是元组,但除此之外,这应该让您开始朝着一个良好的方向发展。