2

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

Sreedhar Danturthi
  • 7,119
  • 19
  • 68
  • 111

3 Answers3

1

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?

Tomas
  • 3,573
  • 2
  • 20
  • 25
1

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();
Gavin
  • 17,053
  • 19
  • 64
  • 110
  • @Gavin this is the error i'm getting if i use your code: Cannot convert type 'System.Linq.IQueryable' to 'int?' – Sreedhar Danturthi May 04 '11 at 13:04
  • @Gavin My query is wrong, it is just getting all the records count not the specific count for occurences of each record in the AllCustomersHistoryOfRecords table – Sreedhar Danturthi May 04 '11 at 13:19
  • So you want an individual count for each component? In which case you probably shouldnt be creating a new component but updating the existing is that right? – Gavin May 04 '11 at 13:22
  • @Gavin yes exactly thats right !! How can i go forward with that ? Help me out !! – Sreedhar Danturthi May 04 '11 at 13:25
  • This is how i changed the query to retrieve the count of each record present in the AllCustomersHistoryOfRecords table: var componentCount = (from c in db.Components join cr in db.CustomersRecords on c.Component_Name equals cr.Software_Title into temporary select new{ Total_Items_Bought = temporary.Count()}); – Sreedhar Danturthi May 04 '11 at 13:26
  • @Gavin I'm trying and i will come back to you as soon i customize it to my application – Sreedhar Danturthi May 04 '11 at 13:41
  • 1
    @gavin It works after little bit of tweaking but i borrowed your idea (which was fantastic) !! thanks a lot for your immediate feedback. Appreciate it !! Have a nice day !! – Sreedhar Danturthi May 04 '11 at 14:18
1

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?'

Community
  • 1
  • 1
Rick james
  • 824
  • 1
  • 11
  • 30