C# 使用WebMatrix显示行计数有困难

C# 使用WebMatrix显示行计数有困难,c#,.net,sql-server,razor,webmatrix,C#,.net,Sql Server,Razor,Webmatrix,我有以下代码: @helper RetrievePhotoWithName(int userid) { var database = Database.Open("SC"); var name = database.QuerySingle("select FirstName, LastName, ProfilePicture from UserProfile where UserId = @0", userid); var notifications = databas

我有以下代码:

@helper RetrievePhotoWithName(int userid)
{
    var database = Database.Open("SC");
    var name = database.QuerySingle("select FirstName, LastName, ProfilePicture from UserProfile where UserId = @0", userid);
    var notifications =  database.Query("SELECT COUNT(*) FROM NotificationsTable WHERE UserID = @0 AND MessageWasRead = @1", userid, false);

    var DisplayName = "";
    if(notifications < 1)
    {
        DisplayName = name["FirstName"] + " " + name["LastName"];
    }
    else
    {
        DisplayName = name["FirstName"] + ", you have " + notifications + " new messages.";
    }
    <a href="@Href("~/Home")" title="My Account"><img src="@Href("~/Shared/Assets/Images/" + name["ProfilePicture"] + ".png")" id="MiniProfilePicture" />&nbsp;@DisplayName</a>

    database.Close();
}
@helper RetrievePhotoWithName(int userid)
{
var database=database.Open(“SC”);
var name=database.QuerySingle(“从UserProfile中选择FirstName、LastName、ProfilePicture,其中UserId=@0”,UserId);
var notifications=database.Query(“从NotificationsTable中选择COUNT(*),其中UserID=@0,MessageWasRead=@1”,UserID,false);
var DisplayName=“”;
如果(通知<1)
{
DisplayName=name[“FirstName”]+“”+name[“LastName”];
}
其他的
{
DisplayName=name[“FirstName”]+,您有“+通知+”新消息。“;
}
Close()数据库;
}
它应该计算我的表中的所有行,或者在网页上向用户显示“新通知”消息,或者只显示他们的名称,但它不起作用

不要编辑此问题。我之所以重新发布此问题,是因为我的上一个问题已更改,导致回答不准确。

由于
选择计数(*)
返回的是单个标量值,我想您应该使用
数据库。QueryValue()

int notifications =  (int)database.QueryValue("SELECT COUNT(*) FROM NotificationsTable WHERE UserID = @0 AND MessageWasRead = @1", userid, false);

由于
选择计数(*)
返回单个标量值,我想您应该使用
数据库。QueryValue()

int notifications =  (int)database.QueryValue("SELECT COUNT(*) FROM NotificationsTable WHERE UserID = @0 AND MessageWasRead = @1", userid, false);

像这样尝试:

@helper RetrievePhotoWithName(int userid)
{
    var database = Database.Open("SC");
    var name = database.QuerySingle("select FirstName, LastName, ProfilePicture from UserProfile where UserId = @0", userid);
    var notifications =  database.Query("SELECT convert(int,COUNT(*)) as 'counter' FROM NotificationsTable WHERE UserID = @0 AND MessageWasRead = @1", userid, false);

    var DisplayName = "";
    if(notifications["counter"] < 1)
    {
        DisplayName = name["FirstName"] + " " + name["LastName"];
    }
    else
    {
        DisplayName = name["FirstName"] + ", you have " + notifications["counter"] + " new messages.";
    }
    <a href="@Href("~/Home")" title="My Account"><img src="@Href("~/Shared/Assets/Images/" + name["ProfilePicture"] + ".png")" id="MiniProfilePicture" />&nbsp;@DisplayName</a>

    database.Close();
}
@helper RetrievePhotoWithName(int userid)
{
var database=database.Open(“SC”);
var name=database.QuerySingle(“从UserProfile中选择FirstName、LastName、ProfilePicture,其中UserId=@0”,UserId);
var notifications=database.Query(“从NotificationsTable中选择convert(int,COUNT(*)作为“计数器”,其中UserID=@0和MessageWasRead=@1”,UserID,false);
var DisplayName=“”;
如果(通知[“计数器”]<1)
{
DisplayName=name[“FirstName”]+“”+name[“LastName”];
}
其他的
{
DisplayName=name[“FirstName”]+,您有“+通知[“计数器”]+”新消息。“;
}
Close()数据库;
}
像这样尝试:

@helper RetrievePhotoWithName(int userid)
{
    var database = Database.Open("SC");
    var name = database.QuerySingle("select FirstName, LastName, ProfilePicture from UserProfile where UserId = @0", userid);
    var notifications =  database.Query("SELECT convert(int,COUNT(*)) as 'counter' FROM NotificationsTable WHERE UserID = @0 AND MessageWasRead = @1", userid, false);

    var DisplayName = "";
    if(notifications["counter"] < 1)
    {
        DisplayName = name["FirstName"] + " " + name["LastName"];
    }
    else
    {
        DisplayName = name["FirstName"] + ", you have " + notifications["counter"] + " new messages.";
    }
    <a href="@Href("~/Home")" title="My Account"><img src="@Href("~/Shared/Assets/Images/" + name["ProfilePicture"] + ".png")" id="MiniProfilePicture" />&nbsp;@DisplayName</a>

    database.Close();
}
@helper RetrievePhotoWithName(int userid)
{
var database=database.Open(“SC”);
var name=database.QuerySingle(“从UserProfile中选择FirstName、LastName、ProfilePicture,其中UserId=@0”,UserId);
var notifications=database.Query(“从NotificationsTable中选择convert(int,COUNT(*)作为“计数器”,其中UserID=@0和MessageWasRead=@1”,UserID,false);
var DisplayName=“”;
如果(通知[“计数器”]<1)
{
DisplayName=name[“FirstName”]+“”+name[“LastName”];
}
其他的
{
DisplayName=name[“FirstName”]+,您有“+通知[“计数器”]+”新消息。“;
}
Close()数据库;
}


name[“FirstName”]是否返回任何内容?能否请您详细解释“它不工作”的含义?这里有一些聪明人,但我们不是读心术的人。它返回我的名字:-),但它只是用来计算未读邮件数的通知查询,不起作用。谢谢@AaronBertrand-我正在尝试(使用notifications查询)计算MessageWasRead列(位数据类型)中的未读邮件数。但它总是给我大量的错误信息,这取决于我的尝试。范围从“无法将类型动态转换为int”和类似的消息请检查我的答案,可能会起作用。否则,请检查@mgnoonan answer.
name[“FirstName”]
是否返回任何内容?能否请您详细解释“它不工作”的含义?这里有一些聪明人,但我们不是读心术的人。它返回我的名字:-),但它只是用来计算未读邮件数的通知查询,不起作用。谢谢@AaronBertrand-我正在尝试(使用notifications查询)计算MessageWasRead列(位数据类型)中的未读邮件数。但它总是给我大量的错误信息,这取决于我的尝试。范围从“无法将类型动态转换为int”和类似的消息请检查我的答案,可能会起作用。否则,请检查@mgnoonan answer.+1我正要写一个类似的备选方案。(表达式应该总是有别名IMHO。)谢谢@aF-我粘贴了它并收到了以下错误:“/”应用程序中的服务器错误。---------------------------------------------------------------编译错误说明:在编译服务此请求所需的资源时出错。请查看以下特定错误详细信息,并适当修改源代码。编译器错误消息:CS0021:无法将带[]的索引应用于“System.Collections.Generic.IEnumerable”类型的表达式。我不明白:-/it's giving on my或@mgnoonan?谢谢@aF。但它给了我以下错误:“编译器错误消息:CS0021:无法应用带[]的索引”对于类型为“System.Collections.Generic.IEnumerable”的表达式+1,我正要编写一个类似的替代方案。(表达式应始终具有别名IMHO。)谢谢@aF-我将其粘贴到中,并收到以下错误:“应用程序中的服务器错误。---------------------------------------------------------------编译错误说明:在编译服务此请求所需的资源时出错。请查看以下特定错误详细信息,并适当修改源代码。编译器错误消息:CS0021:无法将带[]的索引应用于“System.Collections.Generic.IEnumerable”类型的表达式。我不明白:-/it's giving on my或@mgnoonan?谢谢@aF。但它给了我以下错误:“编译器错误消息:CS0021:无法应用带[]的索引”对于类型为“System.Collections.Generic.IEnumerable”的表达式,@mgnoonan-这是我最初使用的,也是我一直使用的,但今天它不起作用,而且