Copied to clipboard

Flag this post as spam?

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


  • James Feeney 26 posts 166 karma points
    Oct 15, 2013 @ 10:55
    James Feeney
    0

    singleOrDefault syntax issue

    Having trouble with figuring out the proper use of .SingleOrDefault()

    I have two strings: collect and type

    I need to check item(s) have the .Name equaling collect and also check a property value of it equals type

    At the moment I have this:

    CurrentModel.DescendantsOrSelf().Items.SingleOrDefault(x => x.Name == collect);

    This works fine, but I need to include the "and has property value of type"

    I was working with the assumption of this:

    CurrentModel.DescendantsOrSelf().Items.SingleOrDefault(x => x.Name == collect && GetPropertyValue(propertyName) == type);

  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Oct 15, 2013 @ 11:09
    Ali Sheikh Taheri
    0

    try this

    CurrentModel.DescendantsOrSelf().Items.SingleOrDefault(x => x.Name == collect && x.GetPropertyValue<string>(propertyName) == type);
    
  • James Feeney 26 posts 166 karma points
    Oct 15, 2013 @ 11:25
    James Feeney
    0

    Thanks Ali - I'm getting this error now:

     The non-generic method 'umbraco.MacroEngines.DynamicNode.GetPropertyValue(string)' cannot be used with type arguments

    Here's a little bit more code:

    string collect = String.Format("{0}", Request.QueryString["Bottle"]);
    string type = String.Format("{0}", Request.QueryString["Type"]);
    DynamicNode rep = null;
    rep = CurrentModel.DescendantsOrSelf().Items.SingleOrDefault(x => x.Name == collect && x.GetPropertyValue<string>(recipeType) == type);
  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Oct 15, 2013 @ 11:28
    Ali Sheikh Taheri
    0

    I see you are using umbraco 4.

    can you please try this (removed <string>)

    CurrentModel.DescendantsOrSelf().Items.SingleOrDefault(x => x.Name == collect && x.GetPropertyValue(propertyName) == type);
    
  • James Feeney 26 posts 166 karma points
    Oct 15, 2013 @ 11:42
    James Feeney
    0

    oo, getting close!

    I'm now getting an error

    Exception: System.NullReferenceException: Object reference not set to an instance of an object.

    from:

    for (i = 0; i < rep.Children.Count() / numInRow; i++) { 

    a loop to do something to each of the collected items in rep. Can I not use .Count() anymore?

    I am running Umbraco v6.1.3 (Assembly version: 1.0.4954.19342)

     

  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Oct 15, 2013 @ 11:47
    Ali Sheikh Taheri
    0

    it seems that rep is null and you are trying to get the children of it.

    so you should check for null before loop

    if(rep != null)
    {
        for (i = 0; i < rep.Children.Count() / numInRow; i++) 
        { 
         // your logic 
        }
    }
    
  • James Feeney 26 posts 166 karma points
    Oct 15, 2013 @ 12:11
    James Feeney
    0
    rep =CurrentModel.DescendantsOrSelf().Items.SingleOrDefault(x => x.Name== collect && x.GetPropertyValue<string>(recipeType)== type);

    Shouldn't this populate rep so that it's no longer null?

  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Oct 15, 2013 @ 12:15
    Ali Sheikh Taheri
    0

    it depends on your data to be honest.

  • James Feeney 26 posts 166 karma points
    Oct 15, 2013 @ 12:47
    James Feeney
    0

    sorry but I'm completetly confused now. 


    If I define DynamicNode rep = null;

    then define rep = a collection of items

    Why would it matter what type of data it is?

  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Oct 15, 2013 @ 14:53
    Ali Sheikh Taheri
    0

    the problem here is that the "a collection of items" are null.

    to be frank it is really difficult as I am not sure what you would like to do and what data structure you have in Umbraco

Please Sign in or register to post replies

Write your reply to:

Draft