0

I've seen what seem to be similar questions on here but they all seemed very contextual and I've so far had some trouble figuring this out. Still pretty new to C# and razor templating so apologies if this is a repeat question or ridiculously simple!

What I'm trying to do is take the value of a user defined string inside of a foreach loop, convert it to (I assume) an int, add it up for each instance of the loop, and then check the sum of the property.

Here is a dumbed down code example that might make this clearer:

foreach (var col in columnOptions.Columns) 
{
    <div class="@col.Width">
        @*content*@
    </div>  
}
if (totalWidthValue > 12) 
{
    @*do something else*@
}

So basically I need to take the value of col.Width, convert it to an int so it can be calculated, add up the total of each occurrence, and then check whether the total is greater than 12.

Also, in case it helps, this is where/how width is getting set:

[JsonSchema(Title = "Column width", Description = "Select the width of this column based on a 12 column wide grid")]
[PropertyOrder(11)]
[JsonOptions(GridColumns = 6)]
[JsonEnumValues("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12")]
[DefaultValue("12")]
[Localizable(false)]
public string Width { get; set; }

EDIT: Here is what I can provide in terms of my attempts, I fear it may not be fully formed enough to pinpoint any specific "here's where you went wrong", but at least it shows an attempt and maybe gives some context to where my knowledge gaps are (the added parts really are just the two variables):

    foreach (var col in columnOptions.Columns) 
    {

        var colNum = double.TryParse(col.Width, out colNum);
        var totalWidthValue = colNum + colNum;

        <div class="@col.Width">
            @*content*@
        </div>  
    }
    if (totalWidthValue > 12) 
    {
        @*do something else*@
    }
tganyan
  • 603
  • 3
  • 9
  • 23
  • Have you tried anything? – Gilad Green Apr 17 '17 at 17:50
  • Nothing that isn't erroring. Truth be told I'm pretty unsure about how to approach it from the get-go and I felt including any failed attempts of my own would just muddy the waters too much. In terms of just simply how should this be done, I felt my attempts were mostly irrelevant to that question. – tganyan Apr 17 '17 at 18:01
  • and still it is best to show what you have tried because otherwise this looks just like a "do my work for me" question – Gilad Green Apr 17 '17 at 18:02
  • But what if I'm not even sure where to start? This is a question/answer forum, no? I'll look at what I have attempted and see if I can add it in in a way that makes sense. – tganyan Apr 17 '17 at 18:03
  • you want to do it with linq. Please supply a snippet of code showing you tried with linq. It is fine if it isn't working, that is where we come in but this is not supposed to be a site where people post without showing concrete effort. You don't need to add it all, just the relevant section – Gilad Green Apr 17 '17 at 18:06
  • Ok, updated with the parts I did attempt. I'm still certain there are concepts here I'm either missing or not understanding, particularly I'm not convinced "colNum + colNum" is the way I want to be adding that up. – tganyan Apr 17 '17 at 18:17

1 Answers1

1

For your attempt: The problem is that using TryParse it returns a bool and has an out parameter. Then for the accumulating of the sum use +=. So instead of this:

var colNum = double.TryParse(col.Width, out colNum);
var totalWidthValue = colNum + colNum;

Have something like this:

double colNum;
if(double.TryParse(col.Width, out colNum))
    totalWidthValue += colNum;

Use linq's .Sum to do so:

int desiredWidth = 12;
if(columnOptions.Columns.Sum(col => int.Parse(col.Width)) > desiredWidth)
{
    /*  ... */
}

If you are not certain that the width is a valid int you can do something similar to the second code snipet in this answer:

Func<string, int, int> parseIntOrDefault = (input, defaultValue) =>
{
    int value = defaultValue;
    int.TryParse(input, out value);
    return value;
};

columnOptions.Columns.Sum(col => parseIntOrDefault(col.Width,0)))
Community
  • 1
  • 1
Gilad Green
  • 36,708
  • 7
  • 61
  • 95
  • This looks very useful, will give it a shot. Thanks for the answer and also thanks for the tips on how to make my question better! – tganyan Apr 17 '17 at 18:18
  • @tganyan - see update with what was wrong with your approach. Sorry for being pedant with you first posting what you tried but it is part of what helps SO be both a great place for finding answer but also a tool for learning – Gilad Green Apr 17 '17 at 18:20
  • 1
    Absolutely, I don't take it personally but just an opportunity to continue learning what kind of info is important on these sorts of posts. – tganyan Apr 17 '17 at 18:24