Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Mark Olbert 87 posts 117 karma points
    Oct 22, 2009 @ 05:19
    Mark Olbert
    0

    Count of Members?

    How do I do a count of members, other than by retrieving all of them and examining the length of the array?

    Also, is it possible to do filtered counts (i.e., count of members with a property value == something)?

    - Mark

  • skiltz 501 posts 701 karma points
    Oct 22, 2009 @ 05:40
    skiltz
    0
    try someting like:






    var Members = Member.GetAll.Count(g=>g.getProperty("City") == "San Carlos")

  • skiltz 501 posts 701 karma points
    Oct 22, 2009 @ 05:41
    skiltz
    0
    var Members = Member.GetAll.Count(g=>g.getProperty("City").Value.ToString() == "San Carlos")
  • Aaron Powell 1708 posts 3044 karma points c-trib
    Oct 22, 2009 @ 05:53
    Aaron Powell
    2

    You should probably assign the Member.GetAll to a local variable before doing the lambda expression. I'm not quite sure how the GetEnumerator() method is evaluated by the runtime engine but the GetAll accessor executes a database query. For example these two statements hit the database which returns every record each time:

    var first = Member.GetAll[0];
    var count = Member.GetAll.Length;

    If you have 100 members all 100 members are returned each time, regardless of what you want.

    But if I was to do this:

    var members = Members.GetAll;
    var first = members[0];
    var count = members.Length

    We only do a single DB call (yes, to return all 100 members though) rather than two.

  • Mark Olbert 87 posts 117 karma points
    Oct 22, 2009 @ 07:13
    Mark Olbert
    0

    Interesting, thanks. Right now I'm executing a scalar sqlserver function that I put into the database:

    ALTER FUNCTION [dbo].[sfNumberOfMembers]
    (
    )
    RETURNS int
    AS
    BEGIN
        DECLARE @retVal int = (SELECT COUNT(nodeId) FROM cmsMember)

        RETURN @retVal

    END

    I'm pretty sure this is valid, although it's undesirable to be tied that closely to the table structure in case it changes.

    - Mark

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies