使用 LINQtoSQL 将记录计数值插入数据库表

发布于 2024-11-05 10:55:18 字数 964 浏览 3 评论 0原文

我有一个名为 AllCustomersHistoryOfRecords 的数据库表,该表包含访问我的应用程序/网站的所有用户购买的所有记录的历史记录。还有另一个名为 Components 的表,它只列出了我网站上可用(可供下载)的所有组件。现在,我想计算 AllCustomersHistoryOfRecords 中的所有可用记录,并更新 Components 表的 *Total_Downloads* 字段中的计数。以下是我正在做的事情:

主要是计算 AllCustomersHistoryOfRecords 表中每条记录的出现次数。

var componentCount = from c in db.Components
                                 join cr in db.AllCustomersHistoryOfRecords
                                 on c.Component_Name equals cr.Software_Title into temporary
                                 select temporary.Count();

这是我用来将数据插入 Components 表的代码:

Component comp = new Component { Total_Downloads = componentCount};
db.Components.InsertOnSubmit(comp);

但问题是我收到以下错误:

Cannot implicitly convert type 'System.Linq.IQueryable<int>' to 'int?'

我该如何解决这个问题?请帮助我!

感谢期待

I have a database table called AllCustomersHistoryOfRecords which is table with history of all the records bought by all the users who visit my application/website. There is another table called Components which just enlists all the components available (for downloading) from my website. Now i want to count all the records available in AllCustomersHistoryOfRecords and update the count in field *Total_Downloads* of Components table. Here is what i'm doing to accomplish this:

Which is primarily counting the occurences of each record in AllCustomersHistoryOfRecords table.

var componentCount = from c in db.Components
                                 join cr in db.AllCustomersHistoryOfRecords
                                 on c.Component_Name equals cr.Software_Title into temporary
                                 select temporary.Count();

And this is the code i'm using to insert the data into Components table:

Component comp = new Component { Total_Downloads = componentCount};
db.Components.InsertOnSubmit(comp);

But the problem is i'm getting the following error:

Cannot implicitly convert type 'System.Linq.IQueryable<int>' to 'int?'

How can i solve this problem ? Please Help me out!!

Thanks in anticipation

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

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

发布评论

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

评论(3

以往的大感动 2024-11-12 10:55:18

我猜测 componentCount 字段是一个可为空的字段?

如果是这种情况,您需要将 componentCount 转换为可为 null 的 int,并从 linq 查询返回结果集而不是 IQueryable。

    var componentCount = (from c in db.Components
                                     join cr in db.AllCustomersHistoryOfRecords
                                     on c.Component_Name equals cr.Software_Title into temporary
                                     select c).Count();
    Component comp = new Component { Total_Downloads = (int?)componentCount};
    db.Components.InsertOnSubmit(comp);

编辑 循环遍历组件并更新计数

您需要将 c.ComponenetId 和 comp.ComponentId 替换为组件表/对象上的主键。由于我还没有运行过,可能会出现一些小问题,但它应该可以让您很好地了解如何实现您想要的目标。

var components = (from c in db.components select c).ToList();
foreach(var comp in components)
{
    var componentCount = (from c in db.Components
                         join cr in db.AllCustomersHistoryOfRecords
                         on c.Component_Name equals cr.Software_Title into temporary
                         where c.ComponentId == comp.ComponentId
                         select c).Count();
    comp.Total_Downloads = (int?)componentCount;                
}
db.SubmitChanges();

I'm guessing the componentCount field is a nullable field?

If this is the case you need to cast componentCount to a nullable int and also return a result set from the linq query rather than an IQueryable.

    var componentCount = (from c in db.Components
                                     join cr in db.AllCustomersHistoryOfRecords
                                     on c.Component_Name equals cr.Software_Title into temporary
                                     select c).Count();
    Component comp = new Component { Total_Downloads = (int?)componentCount};
    db.Components.InsertOnSubmit(comp);

EDIT Looping through components and updating counts

You'll need to replace c.ComponenetId and comp.ComponentId with what ever is the primary key on the component table/object. There may be some minor issues as I havent run this but it should give you a good idea of how to achieve what you are after.

var components = (from c in db.components select c).ToList();
foreach(var comp in components)
{
    var componentCount = (from c in db.Components
                         join cr in db.AllCustomersHistoryOfRecords
                         on c.Component_Name equals cr.Software_Title into temporary
                         where c.ComponentId == comp.ComponentId
                         select c).Count();
    comp.Total_Downloads = (int?)componentCount;                
}
db.SubmitChanges();
李不 2024-11-12 10:55:18

您是否尝试过在查询中调用 .First() ,以便结果不会作为 IQuerable 返回。这一点在这篇文章中也有说明。

无法将类型“System.Linq.IQueryable”隐式转换为“int” ?'

Have you tried calling .First() on your query so the result is not returned as an IQuerable. This is also stated in this post.

Cannot implicitly convert type 'System.Linq.IQueryable' to 'int?'

对你的占有欲 2024-11-12 10:55:18

那里不应该有一组括号吗?

var componentCount = (from c in db.Components
                             join cr in db.AllCustomersHistoryOfRecords
                             on c.Component_Name equals cr.Software_Title into temporary
                             select temporary).Count();

编辑:
如果我理解正确的话,你是想得到所有计数的总和?
如果正确,那么这个怎么样:

var componentCount = (from c in db.Components
                             join cr in db.AllCustomersHistoryOfRecords
                             on c.Component_Name equals cr.Software_Title into temporary
                             select temporary.Count()).Sum();

如果不正确,那么您能描述一下您想做什么吗?

Should there not be a set of parentheses there?

var componentCount = (from c in db.Components
                             join cr in db.AllCustomersHistoryOfRecords
                             on c.Component_Name equals cr.Software_Title into temporary
                             select temporary).Count();

EDIT:
If I understand correctly, you are trying to get the sum all all the counts?
If correct then how about this:

var componentCount = (from c in db.Components
                             join cr in db.AllCustomersHistoryOfRecords
                             on c.Component_Name equals cr.Software_Title into temporary
                             select temporary.Count()).Sum();

If not, then can you please describe what you want to do?

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