使用 LINQtoSQL 将记录计数值插入数据库表
我有一个名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我猜测 componentCount 字段是一个可为空的字段?
如果是这种情况,您需要将 componentCount 转换为可为 null 的 int,并从 linq 查询返回结果集而不是 IQueryable。
编辑 循环遍历组件并更新计数
您需要将 c.ComponenetId 和 comp.ComponentId 替换为组件表/对象上的主键。由于我还没有运行过,可能会出现一些小问题,但它应该可以让您很好地了解如何实现您想要的目标。
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.
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.
您是否尝试过在查询中调用 .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?'
那里不应该有一组括号吗?
编辑:
如果我理解正确的话,你是想得到所有计数的总和?
如果正确,那么这个怎么样:
如果不正确,那么您能描述一下您想做什么吗?
Should there not be a set of parentheses there?
EDIT:
If I understand correctly, you are trying to get the sum all all the counts?
If correct then how about this:
If not, then can you please describe what you want to do?