Copied to clipboard

Flag this post as spam?

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


  • Claushingebjerg 939 posts 2574 karma points
    Sep 21, 2018 @ 10:25
    Claushingebjerg
    0

    Split array and take one

    I have a list of uploaded files in a textarea datatype. I want to split this list, and take the first item in the array...

    @foreach (string pic in @node.GetPropertyValue("billeder").ToString().Split(',').Take(1)) {
       <p>@pic</p>
    }
    

    Gives me an error... Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'System.Array' does not contain a definition for 'Take'

    What to do?

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Sep 21, 2018 @ 10:33
    Dave Woestenborghs
    0

    Hi Claus

    I think array's don't have the Take method. If you call a ToList() it will probably work

    @foreach (string pic in @node.GetPropertyValue("billeder").ToString().Split(',').ToList().Take(1)) {
       <p>@pic</p>
    }
    

    Dave

  • Claushingebjerg 939 posts 2574 karma points
    Sep 21, 2018 @ 10:37
    Claushingebjerg
    0

    Hmmm nope

    Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'System.Array' does not contain a definition for 'ToList'

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Sep 21, 2018 @ 10:40
    Dave Woestenborghs
    0

    I guess you need to add the following using statement to your view

    using System.Linq;
    

    But the ToList() is not needed then I think

    Dave

  • Claushingebjerg 939 posts 2574 karma points
    Sep 21, 2018 @ 10:44
    Claushingebjerg
    0

    Now i have:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage<ContentModels.Somethinsomething>
    @using ContentModels = Umbraco.Web.PublishedContentModels;
    @using System.Linq;
    

    in the top of my file - This is not a partial, but a template.

    But i still get the error: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'System.Array' does not contain a definition for 'Take'

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Sep 21, 2018 @ 10:50
    Dave Woestenborghs
    0

    Hi Claus,

    May I ask why you are using the GetPropertyValue syntax ? I see you have models builder enabled.

    What type of property is billeder ? And of course what @node is ?

    Dave

  • Claushingebjerg 939 posts 2574 karma points
    Sep 21, 2018 @ 11:05
    Claushingebjerg
    0

    Absolutely, and thank you for taking time to help out.

    I am royally confused about Modelsbuilder, so i use what works

    @foreach (string pic in @node.Billeder.ToString().Split(','))
    

    works as well

    "billeder" is a "Textarea" datatype

    enter image description here

    @node is the first child from a node picked with MNTP

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Sep 21, 2018 @ 11:07
    Dave Woestenborghs
    0

    Do you have it working now ? Or not

    Because I tried something similar and didn't even need to include the using statement for system.Linq

    Dave

  • Claushingebjerg 939 posts 2574 karma points
    Sep 21, 2018 @ 11:16
    Claushingebjerg
    0

    Nope, im still getting: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'System.Array' does not contain a definition for 'Take'

    when doing:

    @foreach (string pic in @node.Billeder.ToString().Split(',').Take(1)) { }
    

    and Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'System.Array' does not contain a definition for 'Tolist'

    when doing

    @foreach (string pic in @node.Billeder.ToString().Split(',').Tolist().Take(1)) { }
    
  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Sep 21, 2018 @ 11:19
    Dave Woestenborghs
    0

    Hi Claus,

    Maybe try to add the following using statement as well

    @using System.Collections.Generic;
    

    Dave

  • Claushingebjerg 939 posts 2574 karma points
    Sep 21, 2018 @ 11:20
    Claushingebjerg
    0

    No difference :I - same errors

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Sep 21, 2018 @ 11:25
    Dave Woestenborghs
    0

    Hmmm,

    Could what is the output when you don't use the take ?

    And what do you get when do : @node.Billeder.ToString().Split(,).GetType().FullName

    Dave

  • Claushingebjerg 939 posts 2574 karma points
    Sep 21, 2018 @ 11:33
    Claushingebjerg
    0

    Without take:

    enter image description here

    @node.Billeder.ToString().Split(,).GetType().FullName
    

    returns an "argument missing" but

    @node.Billeder.ToString().GetType().FullName
    

    return "System.String"

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Sep 21, 2018 @ 11:42
    Dave Woestenborghs
    0

    And what does this return

    @node.Billeder.ToString().Split(new [] ',').GetType().FullName
    
  • Claushingebjerg 939 posts 2574 karma points
    Sep 21, 2018 @ 12:01
    Claushingebjerg
    0

    Im a bit confused

    You just want to know the value of @node.Billeder.ToString().Split(new [] ',').GetType().FullName, right. Not used in a ForEach...

    If so, it returns

    Compiler Error Message: CS1514: { expected
    
  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Sep 21, 2018 @ 12:08
    Dave Woestenborghs
    0

    Hi Claus;

    Probably mistyped it. It should be

    @node.Billeder.ToString().Split(new [] { ',' }).GetType().FullName
    

    Dave

  • Claushingebjerg 939 posts 2574 karma points
    Sep 21, 2018 @ 12:11
    Claushingebjerg
    0

    Returns

    System.String[]
    
  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Sep 21, 2018 @ 12:16
    Dave Woestenborghs
    0

    So we confirmed that it is indeed an array

    So now we need to figure out why the Take(1) does not work.

    Dave

  • Claushingebjerg 939 posts 2574 karma points
    Sep 21, 2018 @ 12:20
    Claushingebjerg
    0

    One small step for man... :)

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Sep 21, 2018 @ 12:23
    Dave Woestenborghs
    100

    Hi Claus,

    Did some googling and all say is to include a using System.Linq statement

    Coud you try this

    {
    var stringArray = node.Billeder.ToString().Split(',');
    }
    @foreach (string pic in System.Linq.Enumerable.Take(stringArray, 1)) 
    {
    
    }
    

    Calling the Take extension method directly instead of using as an actual extensions method.

    Just to see if it is available.

    Dave

  • Claushingebjerg 939 posts 2574 karma points
    Sep 21, 2018 @ 12:25
    Claushingebjerg
    0

    SUCCESS!

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Sep 21, 2018 @ 12:27
    Dave Woestenborghs
    1

    Woohoo !

    Still strange the extension method is not picked up when add the using statement.

    Dave

Please Sign in or register to post replies

Write your reply to:

Draft