CSHTML/SQL SUM 一行

发布于 2024-11-25 14:17:57 字数 1203 浏览 1 评论 0原文

我知道在 mysql 中它的 SUM(size),但由于某种原因在 razor cshtml 中构建它不一样,我找不到任何地方谈论在 cshtml 中添加或减去 2 个数字。那么,用来累加行大小的正确函数是什么?

代码:

@{
    Page.Title = "Home @";
    var PageTitle = "Home";

    var db = Database.Open("PhotoGallery");
    var shows = db.Query(@"SELECT * FROM Shows").ToList();
    var seasons = db.Query(@"SELECT * FROM Seasons").ToList();
    var episodes = db.Query(@"SELECT * FROM Episodes").ToList();
    var comics = db.Query(@"SELECT * FROM Comics").ToList();
    var artists = db.Query(@"SELECT * FROM Artists").ToList();
    var albums = db.Query(@"SELECT * FROM Albums").ToList();
    var comicsize = db.Query(@"SELECT SUM(size) FROM Comics").ToList();   
    var totalsizeb = comicsize;
}

<h1>@PageTitle</h1>
<p align="center">
    @shows.Count TV Shows | @seasons.Count Seasons | @episodes.Count Episodes | @comics.Count Comics | @artists.Count Artists | @albums.Count Albums<br />
     Bytes |  MB |  GB |  TB
</p>

错误:

Exception Details: System.Data.SqlServerCe.SqlCeException: The specified argument value for the
function is not valid. [ Argument # = 1,Name of function(if known) = SUM ]

I know in mysql its SUM(size), but for some reason building this in razor cshtml its not the same and i cant find anywhere that talks about adding or subtracting 2 numbers in cshtml. So what would be the right function to use to add up rows size?

Code:

@{
    Page.Title = "Home @";
    var PageTitle = "Home";

    var db = Database.Open("PhotoGallery");
    var shows = db.Query(@"SELECT * FROM Shows").ToList();
    var seasons = db.Query(@"SELECT * FROM Seasons").ToList();
    var episodes = db.Query(@"SELECT * FROM Episodes").ToList();
    var comics = db.Query(@"SELECT * FROM Comics").ToList();
    var artists = db.Query(@"SELECT * FROM Artists").ToList();
    var albums = db.Query(@"SELECT * FROM Albums").ToList();
    var comicsize = db.Query(@"SELECT SUM(size) FROM Comics").ToList();   
    var totalsizeb = comicsize;
}

<h1>@PageTitle</h1>
<p align="center">
    @shows.Count TV Shows | @seasons.Count Seasons | @episodes.Count Episodes | @comics.Count Comics | @artists.Count Artists | @albums.Count Albums<br />
     Bytes |  MB |  GB |  TB
</p>

Error:

Exception Details: System.Data.SqlServerCe.SqlCeException: The specified argument value for the
function is not valid. [ Argument # = 1,Name of function(if known) = SUM ]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终难愈 2024-12-02 14:17:57

如果您只对每个表的 Count 感兴趣,那么您应该 SELECT COUNT(*) FROM Shows 等等,而不是仅仅从每个表中提取所有数据算一下。

@{
    Page.Title = "Home @";
    var PageTitle = "Home";

    var db = Database.Open("PhotoGallery");
    var shows = (int)db.QueryValue(@"SELECT Count(*) FROM Shows");
    var seasons = (int)db.QueryValue(@"SELECT Count(*) FROM Seasons");
    var episodes = (int)db.QueryValue(@"SELECT Count(*) FROM Episodes");
    var comics = (int)db.QueryValue(@"SELECT Count(*) FROM Comics");
    var artists = (int)db.QueryValue(@"SELECT Count(*) FROM Artists");
    var albums = (int)db.QueryValue(@"SELECT Count(*) FROM Albums");
    var comicsize = (int)db.QueryValue(@"SELECT SUM(size) FROM Comics");   
    var totalsizeb = comicsize;
}

<h1>@PageTitle</h1>
<p align="center">
    @shows TV Shows | @seasons Seasons | @episodes Episodes | @comics Comics | @artists Artists | @albums Albums<br />
      Bytes |  MB |  GB |  TB
</p>

SUM 仅适用于数字类型,例如 intnumericmoneyfloat > 确保该列是表中的这些类型之一。

If you're only interested in the Count of each table then you should SELECT COUNT(*) FROM Shows and so forth rather than pulling all the data from each table just to get a count.

@{
    Page.Title = "Home @";
    var PageTitle = "Home";

    var db = Database.Open("PhotoGallery");
    var shows = (int)db.QueryValue(@"SELECT Count(*) FROM Shows");
    var seasons = (int)db.QueryValue(@"SELECT Count(*) FROM Seasons");
    var episodes = (int)db.QueryValue(@"SELECT Count(*) FROM Episodes");
    var comics = (int)db.QueryValue(@"SELECT Count(*) FROM Comics");
    var artists = (int)db.QueryValue(@"SELECT Count(*) FROM Artists");
    var albums = (int)db.QueryValue(@"SELECT Count(*) FROM Albums");
    var comicsize = (int)db.QueryValue(@"SELECT SUM(size) FROM Comics");   
    var totalsizeb = comicsize;
}

<h1>@PageTitle</h1>
<p align="center">
    @shows TV Shows | @seasons Seasons | @episodes Episodes | @comics Comics | @artists Artists | @albums Albums<br />
      Bytes |  MB |  GB |  TB
</p>

SUM only works with numeric types such as int, numeric, money, and float make sure that the column is one of those types in your table.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文