gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:29:16 41 4
gpt4 key购买 nike

我遇到了这个错误:

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 但如果它是 string...我该怎么做?

谢谢谢谢

顺便说一句,我正在使用 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'){

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

最佳答案

问题是 foreach 循环的结构倒退了。在 C# 中,它是 identifire in collection

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

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

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

关于c# - 我有这个错误 CS1001 (C#) 和 CS1026 ..但不知道如何解决,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10285597/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com