c# - 我可以将这两个 C# 函数合并为一个吗?

标签 c# linq function

我有以下内容:

    protected SelectList GetReferenceOptions(string pk)
    {
        var options = new SelectList(_reference.Get(pk)
            .AsEnumerable()
            .OrderBy(o => o.Order), "RowKey", "Value");
        return options;
    }

    protected SelectList GetReferenceOptions(string pk, string value)
    {
        var options = new SelectList(_reference.Get(pk)
            .AsEnumerable()
            .OrderBy(o => o.Order), "RowKey", "Value", value);
        return options;
    }

这看起来像是我可以组合成一个的东西,但我不确定如何处理参数。谁能帮我解释一下我该怎么做?

最佳答案

如果在您显示的第一个构造函数中将 value 设置为 null,那么这将起作用:

protected SelectList GetReferenceOptions(string pk, string value = null)
{
    var options = new SelectList(_reference.Get(pk)
        .AsEnumerable()
        .OrderBy(o => o.Order), "RowKey", "Value", value);
    return options;
}

关于c# - 我可以将这两个 C# 函数合并为一个吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10809976/

相关文章:

c++ - 函数计算被跳过的字符串类的字符?

c# - Blazor,如果加载则返回/停止渲染

c# - linq 语法选项

c# - 如何合并这两个 linq 查询的输出?

c - 使用 read 读取时出现段错误

c++ - 在 C++ 中发送指向函数的动态指针数组

c# - JsonConverter CanConvert 不接收类型

c# - 需要引用部分文件名数组的倒数第二个元素

c# - 在 WinRT Windows 应用商店应用程序中通过 OAuth 进行身份验证的最佳方式

c# - 将列表中的项目分配给列表中的另一个项目的最快方法