Copied to clipboard

Flag this post as spam?

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


  • Simon 692 posts 1068 karma points
    Sep 06, 2015 @ 10:33
    Simon
    0

    Get a random number of relations

    Hi Guys,

    Does anybody knows how I can achieve this:

    I have this line of code, where I getting all relations that a country has with a category:

    umbraco.cms.businesslogic.relation.Relation[] categoryRelations = umbraco.cms.businesslogic.relation.RelationType.GetByAlias("categoryToCountryRelation").GetRelations(countryId);
    

    Now, I need not to get all relations , but only 4 random.

    Does anybody know how I can dot his please?

    Thank you.

  • jigar 169 posts 232 karma points
    Sep 06, 2015 @ 13:13
    jigar
    0

    Hi you can use the linq functionality to fetch random from the list.

    e.g

    List<umbraco.cms.businesslogic.relation.Relation> NewRandomRelList = YourList.OrderBy(x => System.Guid.NewGuid()).ToList().Take(4);
    

    So in your case

    List<umbraco.cms.businesslogic.relation.Relation> NewRandomRelList = categoryRelations.OrderBy(x => System.Guid.NewGuid()).ToList().Take(4);
    

    Hope it helps.

    LUV umbraco!

  • Simon 692 posts 1068 karma points
    Sep 06, 2015 @ 13:20
    Simon
    0

    Hi jigar,

    Thank you for your reply.

    But with the first line of code,

    umbraco.cms.businesslogic.relation.Relation[] categoryRelations = umbraco.cms.businesslogic.relation.RelationType.GetByAlias("categoryToCountryRelation").GetRelations(countryId);
    

    I am even getting all the relations, and the selection of the four random is being done after this call. So in the array, I am also getting all relations, even the uncessary.

    May I choose the four random in this line of code where I am getting the relations?

    Thank you.

    Kind Regards.

  • jigar 169 posts 232 karma points
    Sep 06, 2015 @ 13:32
    jigar
    100

    yes you can product the one liner for sure, I was trying to give the quick direction.

    It should look like

    umbraco.cms.businesslogic.relation.Relation[] categoryRelations = umbraco.cms.businesslogic.relation.RelationType.GetByAlias("categoryToCountryRelation").GetRelations(countryId).OrderBy(x => System.Guid.NewGuid()).ToList().Take(4);
    

    Hope it help.

    Luv Umbraco!!!

  • Simon 692 posts 1068 karma points
    Sep 06, 2015 @ 13:34
    Simon
    0

    Can you guide how I can achieve it please?

  • Simon 692 posts 1068 karma points
    Sep 06, 2015 @ 13:54
    Simon
    0

    Oops Jigar, I did not see the full comment .

    Thank you. I will try it :)

    Cheers

  • jigar 169 posts 232 karma points
    Sep 07, 2015 @ 04:51
    jigar
    0

    did that worked for you?

  • Simon 692 posts 1068 karma points
    Sep 07, 2015 @ 06:33
    Simon
    0

    Works Fine :)

    Cheers

  • 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