【问题标题】:Inserting the count value of records into database table using LINQtoSQL使用 LINQtoSQL 将记录的计数值插入数据库表
【发布时间】:2011-08-18 12:30:35
【问题描述】:

我有一个名为 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?'

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

感谢期待

【问题讨论】:

    标签: c# asp.net linq-to-sql record-count


    【解决方案1】:

    我猜 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);
    

    EDIT循环遍历组件并更新计数

    您需要将 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();
    

    【讨论】:

    • @Gavin 这是我在使用您的代码时遇到的错误:无法将类型 'System.Linq.IQueryable' 转换为 'int?'
    • @Gavin 我的查询是错误的,它只是获取所有记录计数而不是 AllCustomersHistoryOfRecords 表中每条记录出现的具体计数
    • 所以您想为每个组件单独计数?在这种情况下,您可能不应该创建一个新组件,而是更新现有组件,对吗?
    • @Gavin 是的,完全正确!我该怎么做呢?帮帮我!!
    • @gavin 经过一点点调整后它就可以工作了,但我借用了你的想法(这太棒了)!非常感谢您的即时反馈。欣赏它!祝你有美好的一天!
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

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

      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();
      

      如果不是,那你能描述一下你想做什么吗?

      【讨论】:

      • 我得到了 gavin 的帮助,非常感谢您的反馈。欣赏!!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-08
      • 2012-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-31
      相关资源
      最近更新 更多