2

我收到了这个错误:

Compiler Error Message: CS1001: Identifier expected

从这组代码中:

var reqcategory=""; 
        foreach(Request["category"] as reqcategory)
        {
        var sql5 = "SELECT Type.PreReq1, Type.PreReq2, (CASE WHEN (Type.PreReq1 IS NOT NULL) AND (PermitApp1.RPClass IS NULL) AND (PermitApp1.RPCategory IS NULL) THEN 1 ELSE 0 END) AS missing1, (CASE WHEN (Type.PreReq2 IS NOT NULL) AND (PermitApp2.RPClass IS NULL) AND (PermitApp2.RPCategory IS NULL) THEN 1 ELSE 0 END) AS missing2 FROM Type LEFT JOIN PermitApp AS PermitApp1 ON (Type.PreReq1=PermitApp1.RPClass) OR (Type.PreReq1=PermitApp1.RPCategory) AND ( PermitApp1.CDSID = @0 ) AND (PermitApp1.MDecision='1') LEFT JOIN PermitApp AS PermitApp2 ON (Type.PreReq2=PermitApp2.RPClass) OR (Type.PreReq2=PermitApp2.RPCategory) AND ( PermitApp2.CDSID = @1 ) AND (PermitApp2.MDecision='1') WHERE Type.PType = @2";
        var result = db.QuerySingle(sql5, myCDSID, myCDSID, reqcategory);
        var miss1 = result.missing1;
        var miss2 = result.missing2;
        }

错误恰好落在这条线上:

foreach(Request["category"] as reqcategory)

正如编译器所强调的那样。

谁能告诉我我的错误是什么?我应该如何声明一个标识符?

无论如何,什么是标识符?我似乎无法理解http://msdn.microsoft.com/en-us/library/b839hwk4(VS.80).aspx中的解释

如果它的 int 我会使用int.parse正确但如果它是字符串...我该怎么做?

谢谢,谢谢

顺便说一句,我正在使用 webmatrix ...

在我使用 JaredPar 的解决方案之后……下一个错误来了……

CS1026: ) expected

在这部分:

if (miss1 == '1' or miss2 == '1'){
            ModelState.AddError("missing", "You have not met the Pre-Requisites for "+ cat +" yet.")
            } else if (miss1 == '0' and miss2 == '0'){

        Session["license"] = Request["licence"];
        Session["from"] = Request["from"];
        Session["to"] = Request["to"];
        Session["group"] = Request["group"];
        Session["class1"] = Request["class1"];
        Session["category1"] = Request["category1"];
        Session["class"] = Request["class"];
        Session["category"] = Request["category"];
        Response.Redirect("~/Questionnaire");
        }

在这条线上:

if (miss1 == '1' or miss2 == '1'){

谢谢......我不明白为什么我需要一个'('......因为我已经关闭了所有它。

4

1 回答 1

3

问题是你有foreach向后循环的结构。在 C# 中,它是identifire in collection.

foreach(var reqcategory in Request["category"]) { 
  ...
} 

请注意,即使这还不够,因为Request[...]返回object不是 C# 中的有效集合类型。您需要指定基础集合的类型或使用dynamic. 最安全的选择是投到IEnumerable

foreach(object reqcategory in (IEnumerable)Request["category"]) { 
  ...
}
于 2012-04-23T17:52:22.003 回答