Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem is with the <code>Select</code> method. In order to sum up all the digits (as described in the algorithm) you would need to return <code>1</code> and <code>0</code> instead of <code>10</code>, <code>1</code> and <code>4</code> instead of <code>14</code> (as in your example). </p> <p>The <strong>easiest</strong> (<strong>but it does not have to be the most optimal</strong>) way to do that is to conert number from <code>Select</code> to <code>string</code> (14 -> "14") and then split the string characters using <code>SelectMany</code>. </p> <p>So your code should look as follows:</p> <pre><code>int mod10sum = number.Reverse() .SelectMany((c, i) =&gt; ((c - '0') &lt;&lt; ((i + 1) &amp; 1)).ToString()) .Sum(c =&gt; c - '0') % 10; checkDigit = (mod10sum == 0 ? 0 : 10 - mod10sum).ToString("0"); Console.WriteLine(checkDigit); </code></pre> <p><strong>A bit of theory</strong></p> <p>LINQ <a href="http://msdn.microsoft.com/en-us/library/bb534336.aspx" rel="nofollow">SelectMany</a> returns <code>IEnumerable&lt;&gt;</code>. When you return <code>string</code> (which is IEnumerable) then that's why <code>SelectMany</code> "splits" returned string into characters. </p> <p>Microsoft has very nice page (<a href="http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b" rel="nofollow">101 LINQ Samples</a>) with different LINQ samples which should help you out.</p> <p><strong>EDIT</strong></p> <p>I would also recommend working on that conversion from <code>int</code> to <code>string</code>. I was working on similar project literally yesterday and in my case that conversion is a bit problematic from performance point of view as we call that method millions of times. If you have to calculate lots of mod10's then it might be not the best solution.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload