Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 调用方法时使用字典参数作为可选参数_C#_Optional Parameters - Fatal编程技术网

C# 调用方法时使用字典参数作为可选参数

C# 调用方法时使用字典参数作为可选参数,c#,optional-parameters,C#,Optional Parameters,我目前正试图在调用方法时使用字典值来命名可选参数。我不确定这在c#中是否可行,但我在使用动态SQL的查询中也做了类似的事情 string[] dobArrayKey = {"dob: "}; string[] dobArrayValue = {txtDob.Text}; string[] ptntNumArrayKey = { "PatientID: " }; string[] ptntNumArrayValue = { txtOfficeMR.Text}; string[] nameArray

我目前正试图在调用方法时使用字典值来命名可选参数。我不确定这在c#中是否可行,但我在使用动态SQL的查询中也做了类似的事情

string[] dobArrayKey = {"dob: "};
string[] dobArrayValue = {txtDob.Text};
string[] ptntNumArrayKey = { "PatientID: " };
string[] ptntNumArrayValue = { txtOfficeMR.Text};
string[] nameArrayKey = { "FirstName: ", "LastName: " };
string[] nameArrayValue = { txtFirstname.Text, txtLastname.Text };

List<List<string>> searchResults = new List<List<string>>();


Dictionary<string[], string[]> searchCriteria = new Dictionary<string[], string[]> 
{ 
    {dobArrayKey,dobArrayValue}
    ,{ptntNumArrayKey,ptntNumArrayValue}
    ,{nameArrayKey,nameArrayValue}
};

foreach (var item in searchCriteria)
{
    if (item.Value[0] != "" && item.Value[0] != null)
    {
        searchResults.Add(new List<string>());

        for (int x = 0; x <= item.Key.Count(); x++)
        {
            string strJSON = doPatientSearch(Convert.ToInt32(au.UserID)
                , Convert.ToInt32(Session["PracticeID"]), au.SessionID, item.Key[x].ToString() : item.Value[x].ToString() );         

            PatientSearchResponse ptLi = JsonConvert.DeserializeObject<PatientSearchResponse>(json2);

            foreach (PatientList3 patient in ptLi.PatientList)
            {
                searchResults[x].Add(patient.PatientNumber);
            }

        }
    }
}

public static string doPatientSearch(int UserID, int PracticeID, string SessionID, string PatientID = null,
        ,string first = null, string last = null, string dob = null,  string social = null)
{
    //search
}
string[]dobArrayKey={“dob:”};
字符串[]dobArrayValue={txtDob.Text};
字符串[]ptntNumArrayKey={“PatientID:”};
字符串[]ptntNumarayValue={txtOfficeMR.Text};
字符串[]nameArrayKey={“FirstName:”,“LastName:”};
字符串[]nameArrayValue={txtFirstname.Text,txtLastname.Text};
列表搜索结果=新列表();
字典搜索条件=新字典
{ 
{dobArrayKey,dobArrayValue}
,{ptntNumArrayKey,ptntNumArrayValue}
,{nameArrayKey,nameArrayValue}
};
foreach(searchCriteria中的var项)
{
if(item.Value[0]!=“”&item.Value[0]!=null)
{
searchResults.Add(新列表());
对于(int x=0;x)传递表达式
由于这些条件是事后使用的(即通过筛选完整的结果集),因此可以使用LINQ筛选结果。为了获得最大的灵活性,调用者可以传入一个将用作每个项目回调的参数,以确定是否应包括该项

要获取筛选的结果集,请执行以下操作:

public IEnumerable<Patient> FindPatients(Func<Patient,bool> criteria)
{
    return sourceData
        .Where (criteria);
}

如您所见,调用者可以提供所需的任何标准,并且具有类型安全和早期绑定的优点。这远远优于使用两者都没有的字典

完整示例代码:

class Patient
{
    public int      UserID     { get; set; }
    public int      PracticeID { get; set; }
    public string   FirstName  { get; set; }
    public string   LastName   { get; set; }
    public DateTime DOB        { get; set; }
    public string   Social     { get; set; }
    public override string ToString()
    {
        return string.Format("{0} {1} {2}", UserID, FirstName, LastName);
    }
}

class PatientRepository
{
    static private readonly List<Patient> sourceData = new List<Patient>
    {
        new Patient
        {
            UserID = 1, PracticeID = 10, FirstName = "John", LastName = "Doe", DOB = DateTime.Parse("1/2/1968"), Social="123456789"
        },
        new Patient
        {
            UserID = 2, PracticeID = 10, FirstName = "Jane", LastName = "Doe", DOB = DateTime.Parse("1/2/1958"), Social="123456790"
        },
        new Patient
        {
            UserID = 3, PracticeID = 10, FirstName = "John", LastName = "Carson", DOB = DateTime.Parse("4/1/1938"), Social="123456791"
        }
    };

    public IEnumerable<Patient> FindPatients(Func<Patient,bool> criteria)
    {
        return sourceData
            .Where (criteria);
    }
    public Patient FindPatient(Func<Patient,bool> criteria)
    {
        return sourceData
            .Single(criteria);
    }
}

public class Program
{
    public static void Main()
    {
        //Get a reference to the data store
        var patients = new PatientRepository();

        Console.WriteLine("Multiple record search");
        var results = patients.FindPatients
        ( 
            p => p.LastName == "Doe" 
        );
        foreach (var p in results)
        {
            Console.WriteLine(p);
        }

        Console.WriteLine("Single record search");
        var singleResult = patients.FindPatient
        (
            p => p.UserID == 1
        );
        Console.WriteLine(singleResult);
    }
}

字典建议参数可以是任何东西。相反,我会使用一个接口,用于任何内部/受保护/私有方法的真正公共方法和类。
iPartientSearchCriteria
PatientSearchCriteria
。搜索是如何实际执行的?在某个时候是否有存储过程调用?@JohnWu未执行任何存储过程。参数用于构建API调用。API调用的签名是什么?最终,您传入的任何参数都需要映射到API,对吗?@JohnWu如果我正确理解您的问题,是的。签名由userID、sessionID和practiceID组成,其中从au对象中检索,我没有将其结构和初始化包括在问题中,因为我没有将其视为密切相关。我试图在循环中的每次迭代中使用字典搜索条件传递不同的可选参数,然后在事后比较结果。
var results = patients.FindPatients( p => p.LastName == "Doe" );
var results = patients.FindPatients
(   
    p =>
    p.LastName.Contains("Doe") && 
    p.PracticeID == 12 
);
    var singleResult = patients.FindPatient( p => p.UserID == 1);
class Patient
{
    public int      UserID     { get; set; }
    public int      PracticeID { get; set; }
    public string   FirstName  { get; set; }
    public string   LastName   { get; set; }
    public DateTime DOB        { get; set; }
    public string   Social     { get; set; }
    public override string ToString()
    {
        return string.Format("{0} {1} {2}", UserID, FirstName, LastName);
    }
}

class PatientRepository
{
    static private readonly List<Patient> sourceData = new List<Patient>
    {
        new Patient
        {
            UserID = 1, PracticeID = 10, FirstName = "John", LastName = "Doe", DOB = DateTime.Parse("1/2/1968"), Social="123456789"
        },
        new Patient
        {
            UserID = 2, PracticeID = 10, FirstName = "Jane", LastName = "Doe", DOB = DateTime.Parse("1/2/1958"), Social="123456790"
        },
        new Patient
        {
            UserID = 3, PracticeID = 10, FirstName = "John", LastName = "Carson", DOB = DateTime.Parse("4/1/1938"), Social="123456791"
        }
    };

    public IEnumerable<Patient> FindPatients(Func<Patient,bool> criteria)
    {
        return sourceData
            .Where (criteria);
    }
    public Patient FindPatient(Func<Patient,bool> criteria)
    {
        return sourceData
            .Single(criteria);
    }
}

public class Program
{
    public static void Main()
    {
        //Get a reference to the data store
        var patients = new PatientRepository();

        Console.WriteLine("Multiple record search");
        var results = patients.FindPatients
        ( 
            p => p.LastName == "Doe" 
        );
        foreach (var p in results)
        {
            Console.WriteLine(p);
        }

        Console.WriteLine("Single record search");
        var singleResult = patients.FindPatient
        (
            p => p.UserID == 1
        );
        Console.WriteLine(singleResult);
    }
}
Multiple record search
1 John Doe
2 Jane Doe
Single record search
1 John Doe