Copied to clipboard

Flag this post as spam?

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


  • Phil Crowe 192 posts 256 karma points
    Aug 13, 2012 @ 16:58
    Phil Crowe
    0

    Fastest way to query members

    I have around 4000 members on my site. I need to be able to search against two properties 'firstname' and 'surname'. I have tried standard linq:

    foreach (var m in umbraco.cms.businesslogic.member.Member.GetAllAsList().Where(m => m.getProperty("firstName").Value.ToString().ToLower().Contains(s) || m.getProperty("lastName").Value.ToString().ToLower().Contains(s)).Take(4))
                {
                    if (m.getProperty("firstName") != null)
                    {
                        sb.Append("<child>");
                        sb.Append("<id>" + m.Id + "</id>");
                        sb.Append("<first>" + m.getProperty("firstName").Value + "</first>");
                        sb.Append("<last>" + m.getProperty("lastName").Value + "</last>");
                        sb.Append("<picture>");
                        if (m.getProperty("profilePicture") != null)
                        {
                            if (m.getProperty("profilePicture").Value != "")
                            {
                                sb.Append(m.getProperty("profilePicture").Value);
                            }
                        }
                        sb.Append("</picture>");
                        sb.Append("</child>");
                    }
                }

     

    Even though i just want 4 results this is very slow. Does anyone know a quicker way to do this?

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Aug 13, 2012 @ 17:06
    Bo Damgaard Mortensen
    0

    Hi Phil,

    Yes, there's indeed a faster way of doing that :-) Check out my reply to this thread: http://our.umbraco.org/forum/developers/api-questions/33736-How-to-populate-all-Members-with-Profile-properties

    In the XPath string, you can specify the conditions you have in your Where() clause.

    This loads the members from the XML cache instead of the database. The Member.GetAllAsList() gets slow at around 100 - 200 members.

    Let me know if this works :-)

    All the best,

    Bo

  • Phil Crowe 192 posts 256 karma points
    Aug 13, 2012 @ 17:18
    Phil Crowe
    0

    Hmm, doesnt seem to much quicker to be honest

    var members = uQuery.GetMembersByXPath("//*");

    foreach(var m in members.Where(m => m.getProperty("firstName").Value.ToString().ToLower().Contains(s) || m.getProperty("lastName").Value.ToString().ToLower().Contains(s)).Take(4))

                {

                    if (m.getProperty("firstName") != null)

                    {

                        sb.Append("<child>");

                        sb.Append("<id>" + m.Id + "</id>");

                        sb.Append("<first>" + m.getProperty("firstName").Value + "</first>");

                        sb.Append("<last>" + m.getProperty("lastName").Value + "</last>");

                        sb.Append("<picture>");

                        if (m.getProperty("profilePicture") != null)

                        {

                            if (m.getProperty("profilePicture").Value != "")

                            {

                                sb.Append(m.getProperty("profilePicture").Value);

                            }

                        }

                        sb.Append("</picture>");

                        sb.Append("</child>");

     

                    }

                }

  • Phil Crowe 192 posts 256 karma points
    Aug 13, 2012 @ 17:25
    Phil Crowe
    1

    sorry! mis read works great!

     

     var members = uQuery.GetMembersByXPath("//* [contains(firstName,'" + s + "') or contains(lastName,'" + s + "')]");

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Aug 13, 2012 @ 17:30
    Hendy Racher
    0

    Hi Phil,

    If you use the power of XPath to do the filtering it'll be quicker :) (atm you're getting all members and then checking each via the API)

     

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Aug 13, 2012 @ 19:41
    Bo Damgaard Mortensen
    0

    Hi Phil,

    As Hendy points out, the filtering should be done in the XPath itself, but I see you've already got that in place ;-)

    Remember to mark this post as 'solved' for future reference if it solved your problem :-)

    All the best,

    Bo

Please Sign in or register to post replies

Write your reply to:

Draft