[英]2sxc - Cannot use c# vars
I'm trying to use a 2sxc view to create some statistics with linq. 我正在尝试使用2sxc视图使用linq创建一些统计信息。 However I can't seem to be able to call any c# var. 但是我似乎无法调用任何C#变量。 Here's an example: 这是一个例子:
@{
if (@Request.QueryString["ano"] == "" || @Request.QueryString["ano"] == null) {
<p>ano not set, using current year (@DateTime.Now.Year)</p>
var year = @DateTime.Now.Year;
} else {
<p>ano set (@Request.QueryString["ano"])</p>
var year = @Request.QueryString["ano"];
}
var items = AsDynamic(App.Data["entity"]);
items = items.Where(p => Convert.ToDateTime(p.data_a).Year == year);
}
This results in 这导致
error CS0103: The name 'year' does not exist in the current context
Am I doing something really stupid again? 我又在做些愚蠢的事吗?
You are declaring variable inside if
statement and referencing it outside if
你在里面变量声明if
声明外引用它if
Now you can rewrite you code like this: 现在,您可以像这样重写代码:
@{
int year = 0;
if (@Request.QueryString["ano"] == "" || @Request.QueryString["ano"] == null) {
<p>ano not set, using current year (@DateTime.Now.Year)</p>
year = @DateTime.Now.Year; // no "var" keyword here!
} else {
<p>ano set (@Request.QueryString["ano"])</p>
year = int.Parse(Request.QueryString["ano"]);
}
var items = AsDynamic(App.Data["entity"]);
items = items.Where(p => Convert.ToDateTime(p.data_a).Year == year);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.