我有一个类型的字典:
IDictionary<foo, IEnumerable<bar>> my_dictionary
Run Code Online (Sandbox Code Playgroud)
bar类看起来像这样:
class bar
{
public bool IsValid {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
如何创建另一个只包含IsValid = true的项目的字典.
我试过这个:
my_dictionary.ToDictionary( p=> p.Key,
p=> p.Value.Where (x => x.IsValid));
Run Code Online (Sandbox Code Playgroud)
上面代码的问题是,如果该键的所有元素都是IsValid = false,则会创建一个空枚举的键.
例如:
my_dictionar[foo1] = new List<bar> { new bar {IsValid = false}, new bar {IsValid = false}, new bar {IsValid = false}};
my_dictionary[foo2] = new List<bar> {new bar {IsValid = true} , new bar{IsValid = false};
var new_dict = my_dictionary.ToDictionary( p=> p.Key,
p=> p.Value.Where (x => x.IsValid));
// Expected new_dict should contain …Run Code Online (Sandbox Code Playgroud) 如何通过数据源更改包含其数据集的下拉列表的值
ddlContacts.DataSource = Data;
ddlContacts.DataBind();
Run Code Online (Sandbox Code Playgroud)
我试过这个但是不行:
$('#<%= rbDepartment.ClientID %>').change(function() {
if ($("input[@name=GroupName]:checked").val() == "IS") {
$('#ddlContactType').val('AM');
}
});
Run Code Online (Sandbox Code Playgroud) 可能重复:
C# - if语句中的赋值
我发现自己做了以下多次
if (e.row.FindControl("myLiteral") is Literal)
{
(e.row.FindControl("myLiteral") as Literal).Text = "textData";
}
Run Code Online (Sandbox Code Playgroud)
有没有办法替换"if"部分并简化setter:
(e.row.FindControl("myLiteral") <some operator that combines is and as> .text = "textData";
Run Code Online (Sandbox Code Playgroud)
编辑:我之前应该提到这一点 - 我想完全删除'if'.
"some operator"应该在内部执行此操作,并且仅当e.row.FindControl是文字时才设置".text"