Copied to clipboard

Flag this post as spam?

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


  • Michael 63 posts 211 karma points
    Aug 06, 2014 @ 10:16
    Michael
    0

    Sort memberlist by custom property

    Hi guys

    I am working on a member list. So far everything works fine. I now want to add sorting on it. I have a custom property on my members that use the numeric datatype in umbraco. The alias on it is sortering. I thought it was a simple as doing .OrderBy("sortering"). Anyway here is my code:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage

    @using Umbraco.Core;
    @using Umbraco.Core.Models;
    @using Umbraco.Core.Services;

    @{
      var memberservice = ApplicationContext.Current.Services.MemberService;
    }
    <div class="medarbejdere">
        @foreach (Member mem in memberservice.GetAllMembers())
        {
          if (mem.GetValue("visPaaKontaktside").ToString() == "1")
          {
          <div class="worker">
            <div class="maPic">
              @if (mem.HasProperty("medarbejderBillede"))
              {
                var dynamicMediaItem = Umbraco.Media(mem.GetValue("medarbejderBillede"));
                <img src="@dynamicMediaItem.GetCropUrl("umbracoFile", "MedarbejderNy")" alt="@dynamicMediaItem.Name" />
              }
            </div>
            <div class="maInfo">
              <p><strong><a href="[email protected]">@mem.Name</a></strong></p>
              <p>@mem.GetValue("medarbejderTitel")</p>
              <p>@mem.GetValue("medarbejderMobil")</p>
              <p><a href="mailto:@mem.Email">@mem.Email</a></p>
              <p>@mem.GetValue("omraadebeskrivelse")</p>
            </div>
          </div>
          }
        }
      </div>

    Any ideas? :)

     

    /Michael

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Aug 06, 2014 @ 12:45
    Jeavon Leopold
    101

    Hi Michael,

    You will need you use a lambda such as this:

    @{
        var memberservice = ApplicationContext.Current.Services.MemberService;
    }
    <div class="medarbejdere">
        @foreach (var mem in memberservice.GetAllMembers().OrderBy(x => x.GetValue<int>("sortering")))
        {
            if (mem.GetValue("visPaaKontaktside").ToString() == "1")
            {
                <div class="worker">
                    <div class="maPic">
                        @if (mem.HasProperty("medarbejderBillede"))
                        {
                            var dynamicMediaItem = Umbraco.Media(mem.GetValue("medarbejderBillede"));
                            <img src="@dynamicMediaItem.GetCropUrl("umbracoFile", "MedarbejderNy")" alt="@dynamicMediaItem.Name" />
                        }
                    </div>
                    <div class="maInfo">
                        <p><strong><a href="[email protected]">@mem.Name</a></strong></p>
                        <p>@mem.GetValue("medarbejderTitel")</p>
                        <p>@mem.GetValue("medarbejderMobil")</p>
                        <p><a href="mailto:@mem.Email">@mem.Email</a></p>
                        <p>@mem.GetValue("omraadebeskrivelse")</p>
                    </div>
                </div>
            }
        }
    </div>
    

    Jeavon

  • Michael 63 posts 211 karma points
    Aug 06, 2014 @ 14:10
    Michael
    0

    I can get it to work if I add a .ToString() like this:

    @foreach (var mem in memberservice.GetAllMembers().OrderBy(x => x.GetValue("sortering").ToString()))

    The problem now is that the number 10-19 gets displayd before number 2. I am guessing it's because it only looks at the first number?

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Aug 06, 2014 @ 15:13
    Jeavon Leopold
    1

    Did you get an error when using it like this?

    x.GetValue<int>("sortering")
    
  • Michael 63 posts 211 karma points
    Aug 06, 2014 @ 15:24
    Michael
    0

    Ahhh yes, that did it.

    Thank you very much Jeavon :D

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Aug 06, 2014 @ 16:10
    Jeavon Leopold
    1

    Brilliant!

    I don't think it would help you with what you are doing here but are you also aware of the new MembershipHelper?

    Jeavon

  • Michael 63 posts 211 karma points
    Aug 07, 2014 @ 08:03
    Michael
    1

    No, but now i am. Thank you very much :)

    Michael

  • MikeD 92 posts 112 karma points
    Feb 27, 2015 @ 19:57
    MikeD
    0

    Using Umbraco 7.2.2, when I try to OrderBy I get the following error:

    error CS1928: 'System.Collections.Generic.IEnumerable' does not contain a definition for 'OrderBy' and the best extension method overload 'Umbraco.Web.PublishedContentExtensions.OrderBy(System.Collections.Generic.IEnumerable, string)' has some invalid arguments

    I have everything setup exactly as in the above example, except that I want to sort on a string field (company).  Can anyone help?

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Feb 27, 2015 @ 20:02
    Jeavon Leopold
    0

    Hi Mike,

    Could you post your code?

    Jeavon

  • MikeD 92 posts 112 karma points
    Feb 27, 2015 @ 20:03
    MikeD
    0
    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @using Umbraco.Core;
    @using Umbraco.Core.Models;
    @using Umbraco.Core.Services;
    @{
    
        var memberService = ApplicationContext.Current.Services.MemberService;
        var members = memberService.GetMembersByGroup("1"); 
    
        string contact = String.Empty; 
        string company = String.Empty;
        string address = String.Empty; 
        string citystzip = String.Empty; 
        string phone = String.Empty; 
        string fax = String.Empty; 
        string cleanphone = String.Empty; 
        string cleanfax = String.Empty; 
        int counter = 1;
    
    }
    
    @foreach ( Member currentMember in members )
    {
    
        try {
            if(currentMember.HasProperty("company"))
            {
                contact = currentMember.GetValue("contact").ToString();
            }
        }
        catch {}
    
        try {
            if(currentMember.HasProperty("company"))
            {
                company = currentMember.GetValue("company").ToString();
            }
        }
        catch {}
    
        try {
            if(currentMember.HasProperty("company"))
            {
                address = currentMember.GetValue("address").ToString();
            }
        }
        catch {}
    
        try {
            if(currentMember.HasProperty("company"))
            {
                citystzip = currentMember.GetValue("citystzip").ToString();
            }
        }
        catch {}
    
        try {
            if(currentMember.HasProperty("company"))
            {
                phone = currentMember.GetValue("phone").ToString();
                phone = phone.Replace("(", "");
                phone = phone.Replace(")", "");
                phone = phone.Replace(" ", "");
                phone = phone.Replace("-", "");
                cleanphone = String.Format( "{0:(###) ###-####}", Convert.ToInt64(@phone) );
            }
        }
        catch {}
    
        try {
            if(currentMember.HasProperty("company"))
            {
                fax = currentMember.GetValue("fax").ToString();
                fax = fax.Replace("(", "");
                fax = fax.Replace(")", "");
                fax = fax.Replace(" ", "");
                fax = fax.Replace("-", "");
                cleanfax = String.Format( "{0:(###) ###-####}", Convert.ToInt64(@fax) );
            } 
        }
        catch {}
    
        <article class="AssociateBox">
            <span class="AssociateName">@company</span><br />
            <span class="AssociateContactInfo">@contact</span><br />
            <span class="AssociateContactInfo">@address</span><br />
            <span class="AssociateContactInfo">@citystzip</span><br />
            <span class="AssociateContactInfo">Phone: @cleanphone</span><br />
            <span class="AssociateContactInfo">Fax: @cleanfax</span><br />
        </article>
        counter++;
    
        if ( counter == 4 )
        {
            <div class="ClearBoth"></div>
            counter = 1;
        }
    
    }
    
  • MikeD 92 posts 112 karma points
    Feb 27, 2015 @ 20:04
    MikeD
    0

    When I try adding the OderBy to GetMembersByGroup("1") I get the error.

Please Sign in or register to post replies

Write your reply to:

Draft