我試圖執行下面的代碼(在LINQPad),和我的LINQ語句得到一個錯誤不能轉換UserQuery枚舉枚舉
不能鍵入
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();
}
在這種情況下,我想...(從規則中的foo ...)FirstOrDefault ...並且如果沒有任何內容返回null。那麼如何?當沒有規則匹配時,當前'FirstOrDefault'返回一個非空值。 – 2012-08-13 18:14:26
@BrentArias:你爲什麼這麼想? FirstOrDefault特別是返回沒有任何內容匹配的'null'的版本。更確切地說,它返回'default(ResultCode)',它是'null'用於引用類型。但是,當你返回一個枚舉時,它不能爲空,所以你的代碼從一開始就是假的。 – 2012-08-14 05:02:57
@BrentArias:請參閱我的回答更新 – 2012-08-14 05:11:19