Copied to clipboard

Flag this post as spam?

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


  • Doogie Talons 183 posts 318 karma points
    Sep 20, 2013 @ 17:25
    Doogie Talons
    0

    Iterating through members based on Custom fields... help required.

    Hi all just trying to get my head around Razor and Members.

    I am trying to count all members from any given area, or list all distinct areas members are in.

     

    So I know how to list all areas such as...

     

    @{

    var members = Member.GetAll;

    foreach(var member in members) 

    {

    <p>@member.getProperty("memberCity").Value</p>

    }

    }

     

    But is there a simple way using Razor to say...

     

    list all distinct memberCity

    and count all where memberCity = Glasgow

    I don't know how to use the getProperty("memberCity").Value anywhere but once a member is in the loop.

    I hope this makes sense to someone, who could point me in the right direction for the syntax or wether this is indeed possible.

     

    Cheers

    Doogie

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Sep 21, 2013 @ 00:08
    Jeavon Leopold
    0

    Hi Doogie,

    You could do something like this:

    var countGlasgow = members.Count(m => m.GetProperty<string>("memberCity") == "Glasgow");
    

    I would also recommend you take a look at the uQuery members methods here as they are very useful.

    Jeavon

  • Charles Afford 1163 posts 1709 karma points
    Sep 21, 2013 @ 21:40
    Charles Afford
    0

    I would not recommend doing things like Member.GetAll();

    Its really expensive in terms of resources.  You dont need to get every result just do a experssion..  Jeavons example is what you need :).

  • Doogie Talons 183 posts 318 karma points
    Sep 22, 2013 @ 10:06
    Doogie Talons
    0

    Really helpful guys just looking on the uQuery section seems to cover most of what I need.

    1 last queston though. As this returns members how for exampe would you go about returning properties of the members.

    Specifically want to list all the unique/distinct memberCity from all members. Is this possible ?

    Basically I want to return the citys that members are in and show a count next to it.

    If you have a code example that would be great as it would help further down the line with other queries I need to do.

    Thanks for your help so far.

    Doogie

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Sep 23, 2013 @ 12:15
    Jeavon Leopold
    100

    Hi Doogie,

    Using a Linq GroupBy, you could do something like:

    @{
        var members = Member.GetAll;
    
        var col = members.GroupBy(x => x.GetProperty<string>("title"));
    
        foreach (var group in col)
        {
            <h2>@group.Key (@group.Count())</h2>
            foreach(var item in group)
            {
                <h3>@item.LoginName</h3>    
            }
        }
    }
    

    There are some great examples of different Linq samples here

    Thanks,

    Jeavon

  • Doogie Talons 183 posts 318 karma points
    Sep 23, 2013 @ 17:49
    Doogie Talons
    0

    Thanks, that all slotted into place. I simple removed title and put in memberCity and it worked perfectly.

    I will be putting these values into google maps api too so the group mechanism is perfect.

    Thanks very much for your help and the links.

    Doogie.

Please Sign in or register to post replies

Write your reply to:

Draft