Entity Sql - Count function

Last post 11-18-2009 4:34 AM by gameover88. 4 replies.

Sort Posts:

  • Entity Sql - Count function

    07-03-2009, 6:17 AM
    • Member
      4 point Member
    • gameover88
    • Member since 11-26-2008, 8:01 AM
    • Akar
    • Posts 5

    Hi. i'm trying to write an e-sql statement that retrieves all records in a table called 'Medias' and also, number of the comments those belong to each Media, by MediaID (in the Comments table). i've tried several statements like this but i always get error:

    SELECT m.MediaTitle, m. SuggestedBy, m.Summary, (Select COUNT(c.MediaID) FROM Comments WHERE c.MediaID = m.MediaID) AS TotalComments FROM Medias AS m

    i know this may sound easy but i'm in trouble with this :(. i would appreciate if you could help. thanks a lot.

    Childhood's over the moment you know you're gonna die.
  • Re: Entity Sql - Count function

    07-03-2009, 8:17 AM
    Answer
    • Contributor
      2,060 point Contributor
    • andreadottor
    • Member since 01-25-2007, 10:21 AM
    • Italy
    • Posts 312
    • TrustedFriends-MVPs

    I don't have try, but in your query missed "as c" near Comments, and you must indicate the name of the class of your ObjectContext

    SELECT
      m.MediaTitle,
      m. SuggestedBy,
      m.Summary,
      (Select COUNT(c.MediaID) FROM YourObjectContext.Comments AS c WHERE c.MediaID = m.MediaID) AS TotalComments
    FROM YourObjectContext.Medias AS m

    If this answers your question, please select 'mark as answer' thanks!



    Andrea Dottor
    Microsoft MVP - ASP/ASP.NET
  • Re: Entity Sql - Count function

    07-03-2009, 8:55 AM
    • Member
      4 point Member
    • gameover88
    • Member since 11-26-2008, 8:01 AM
    • Akar
    • Posts 5

    andreadottor:

    I don't have try, but in your query missed "as c" near Comments, and you must indicate the name of the class of your ObjectContext

     

    i can't believe how i forgot this.) thanks. now i'm writing this statement:

    SELECT m.MediaTitle, (Select COUNT(c.MediaID) FROM MediaPortalEntities.Comments AS c WHERE c.MediaID = m.MediaID) AS TotalComments FROM MediaPortalEntities.Medias AS m

    But during the run time this is what it shows in the datalist:

    MediaTitle: Fight Club
    TotalComments: System.Collections.Generic.List`1[System.Data.Common.DbDataRecord]

    MediaTitle: The Dark Knight
    TotalComments: System.Collections.Generic.List`1[System.Data.Common.DbDataRecord]

    ..

    still can't get the count numbers :S

     

     

    Childhood's over the moment you know you're gonna die.
  • Re: Entity Sql - Count function

    07-03-2009, 9:10 AM
    Answer
    • Contributor
      2,060 point Contributor
    • andreadottor
    • Member since 01-25-2007, 10:21 AM
    • Italy
    • Posts 312
    • TrustedFriends-MVPs

    Is normal the type is a generic list, because you execute a select that can return many values.

    In a my application (that I have a similar situation) I have create a method that check that the list is not empty and return a value (that is the first element).

    In you case, the code can be this:

    protected int GetCount(IList<int> count)
    {
        if (count == null || count.Count < 1)
            return 0;

        return count[0];
    }

     

    or check in debug the correct type.

    If this answers your question, please select 'mark as answer' thanks!



    Andrea Dottor
    Microsoft MVP - ASP/ASP.NET
  • Re: Entity Sql - Count function

    11-18-2009, 4:34 AM
    • Member
      4 point Member
    • gameover88
    • Member since 11-26-2008, 8:01 AM
    • Akar
    • Posts 5

    well it's a little bit late but thanks a lot for your help :)


    Childhood's over the moment you know you're gonna die.
Page 1 of 1 (5 items)