我有这个错误CS1001(C#)和CS1026 ..但不知道如何解决它

时间:2012-04-23 17:49:58

标签: c# razor webmatrix

我收到了这个错误:

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,但如果它是字符串......我怎么能这样做?

谢谢谢谢

BTW我正在使用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'){

谢谢...我不明白为什么我需要一个'('那里......因为我关闭了所有这些。

1 个答案:

答案 0 :(得分:3)

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

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

请注意,尽管Request[...]返回object并且不是C#中的有效收藏类型,但即使这样也不够。您需要指定基础集合的类型或使用dynamic。最安全的选择是投射到IEnumerable

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