以下のコードを (LINQPad で) 実行しようとしていますが、LINQ ステートメントでエラーが発生します
System.Collections.Generic.IEnumerable<UserQuery.ResultCode>
タイプを に暗黙的に変換することはできませんUserQuery.ResultCode
。
なんで?
コードは次のとおりです。
public enum ResultCode {
Big,
Bad,
Messy
}
public class ValidationResult {
public bool IsValid = false;
public ResultCode Code;
public override string ToString(){
return String.Format("IsValid: {0}, Code: {1}",IsValid, Code);
}
}
public class RuleMap {
public Func<bool> Fact;
public ResultCode Code;
public List<int> Actions;
}
public class RulesProcessor{
List<RuleMap> rules;
public RulesProcessor(){
rules = new List<RuleMap>{
new RuleMap {Fact = CheckLeft, Code = ResultCode.Big,
Actions = new List<int> {2, 3}
},
new RuleMap {Fact = CheckRight, Code = ResultCode.Bad,
Actions = new List<int> {1, 2}
},
new RuleMap {Fact = CheckDown, Code = ResultCode.Messy,
Actions = new List<int> {1, 3}
},
};
}
public bool CheckLeft(){
return true;
}
public bool CheckRight(){
return false;
}
public bool CheckDown(){
return true;
}
public ValidationResult EvaluateRulesOnAction(int actionType){
ValidationResult result = new ValidationResult();
ResultCode badRule = from foo in rules
where !foo.Fact() && foo.Actions.Contains(actionType)
select foo.Code;
if (badRule != null){
result.Code = badRule;
}else{
result.IsValid = true;
}
return result;
}
}
void Main()
{
var foo = new RulesProcessor();
foo.EvaluateRulesOnAction(1).Dump();
foo.EvaluateRulesOnAction(3).Dump();
}