title: IEnumerable Model Object into Array
body: <p>I am learning C# and dotnet core as I want to move away from PHP for my web apps. I am having a specific problem here and I can't get my head around it.</p>
  
  <p>I will show you all the code below, but here is a summary of what I want to achieve: I am getting a table from SQL database. I can display the table in full on my page using foreach loop. However when I am trying to convert the result into an array so I can access a specific item from the returned table I get into all sorts of errors. </p>
  
  <p>By the way I was using this tutorial to supplement my Udemy learning course:
  <a href="https://docs.efproject.net/en/latest/platforms/aspnetcore/existing-db.html" rel="nofollow noreferrer">https://docs.efproject.net/en/latest/platforms/aspnetcore/existing-db.html</a></p>
  
  <p>Might be worth mentioning that I was reverse engineering the Models from existing database (as it is in the linked tutorial above)</p>
  
  <p>Here are my files, I'll explain errors below:</p>
  
  <p>Model:</p>
  
  <pre><code>namespace ASPNET_Core_1_0.Models
  {
      public partial class Graph
      {
          public int AutoId { get; set; }
          public int Numbers { get; set; }
      }
  
  }
  </code></pre>
  
  <p>ModelContext:</p>
  
  <pre><code>using System;
  using Microsoft.EntityFrameworkCore;
  using Microsoft.EntityFrameworkCore.Metadata;
  
  namespace ASPNET_Core_1_0.Models
  {
      public partial class GraphsContext : DbContext
      {
          public virtual DbSet&lt;Graph&gt; Graph { get; set; }
  
  
          public GraphsContext(DbContextOptions&lt;GraphsContext&gt; options)
              : base(options)
          { }
  
  
          protected override void OnModelCreating(ModelBuilder modelBuilder)
          {
              modelBuilder.Entity&lt;Graph&gt;(entity =&gt;
              {
                  entity.HasKey(e =&gt; e.AutoId)
                      .HasName("PK_Blog");
              });
          }
      }
  }
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Threading.Tasks;
  using Microsoft.AspNetCore.Mvc;
  using ASPNET_Core_1_0.Models;
  using Microsoft.EntityFrameworkCore;
  
  // For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860
  
  namespace ASPNET_Core_1_0.Controllers
  {
      public class GraphsController : Controller
      {
          private GraphsContext _context;
  
          public GraphsController(GraphsContext context)
          {
              _context = context;
          }
  
          public IActionResult Index()
          {
              return View(_context.Graph.ToArray());
          }
  
          public IActionResult GraphJ()
          {
              var graphs = _context.Graph.FromSql("SELECT * FROM dbo.Graph").ToList();
              ViewBag.YourHtml = graphs;
  
              return View(_context.Graph.ToList());
          }
  
      }
  }
  </code></pre>
  
  <p>In MS tutorial the Index view used ToList() method:</p>
  
  <pre><code>    public IActionResult Index()
      {
          return View(_context.Graph.ToList());
      }
  </code></pre>
  
  <p>I changed it to ToArray() in hope it will behave like one :/</p>
  
  <p>Index View</p>
  
  <pre><code>@model IEnumerable&lt;ASPNET_Core_1_0.Models.Graph&gt;
  
  @{
      ViewBag.Title = "Graphs";
  }
  
  &lt;h2&gt;Graphs&lt;/h2&gt;
  
  
  
  &lt;table class="table"&gt;
      &lt;tr&gt;
          &lt;th&gt;Id&lt;/th&gt;
          &lt;th&gt;Numbers&lt;/th&gt;
      &lt;/tr&gt;
  
      @foreach (var item in Model)
      {
          &lt;tr&gt;
              &lt;td&gt;
                  @Html.DisplayFor(modelItem =&gt; item.AutoId)
              &lt;/td&gt;
              &lt;td&gt;
                  @Html.DisplayFor(modelItem =&gt; item.Numbers)
              &lt;/td&gt;
          &lt;/tr&gt;
      }
  &lt;/table&gt;
  
  @{ 
  
      // int[] arr = Model.ToArray();
  
      // int[] array = Model.Cast&lt;int&gt;().ToArray();
      // int somenumber = array[5];
  }
  
  &lt;div class="row"&gt;
      &lt;div class="col-md-12 text-center"&gt;
          @Html.DisplayForModel(Model)
          &lt;br /&gt;
  
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>The problem:</strong></p>
  
  <p>If you look at my index view, there are three commented out lines where I was trying to cast the Model object to array so I could access each item in the returned table independently.</p>
  
  <p>Upon running the project I am getting an error:</p>
  
  <blockquote>
    <p>An unhandled exception occurred while processing the request.</p>
    
    <p>InvalidCastException: Unable to cast object of type
    'ASPNET_Core_1_0.Models.Graph' to type 'System.Int32'.</p>
  </blockquote>
  
  <p><strong>Question:</strong>
  How can I get Model object into an array so I can display specific item from the table instead of the whole table.</p>
  
  <p>Say I want to return Value field for ID 4?</p>
  
  <p>I understand this most likely is a rookie question, but I am having real trouble getting or even understanding the answers I see online.</p>
  
  <p><strong>Table data</strong></p>
  
  <p>My table data is simple two column key -> value pair. ID number and corresponding value. </p>
  
  <pre><code>ID Value
  1  10
  2  20
  3  30
  4  40
  5  50 
  </code></pre>
  
  <p>ect</p>
  
  <p><strong>Research</strong></p>
  
  <p><a href="https://stackoverflow.com/questions/268671/best-way-to-convert-ilist-or-ienumerable-to-array">Best way to convert IList or IEnumerable to Array</a></p>
  
  <p><a href="https://stackoverflow.com/questions/6570334/convert-ienumerableint-to-int">Convert IEnumerable&lt;int&gt; to int[]</a></p>
  
  <p><a href="https://stackoverflow.com/questions/22382601/error-cannot-implicitly-convert-type">Error: &quot;Cannot implicitly convert type&quot;</a></p>
  
  <p><a href="https://stackoverflow.com/questions/2640738/c-sharp-cannot-implicitly-convert-type-listproduct-to-listiproduct">C# - Cannot implicitly convert type List&lt;Product&gt; to List&lt;IProduct&gt;</a></p>
  
  <p><a href="https://www.tutorialspoint.com/csharp/csharp_arrays.htm" rel="nofollow noreferrer">https://www.tutorialspoint.com/csharp/csharp_arrays.htm</a></p>
  
  <p><a href="https://www.dotnetperls.com/array" rel="nofollow noreferrer">https://www.dotnetperls.com/array</a></p>
  
  <p><a href="https://www.dotnetperls.com/list" rel="nofollow noreferrer">https://www.dotnetperls.com/list</a></p>
  
  <p><a href="https://stackoverflow.com/questions/9854917/how-can-i-find-a-specific-element-in-a-listt">How can I find a specific element in a List&lt;T&gt;?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/1216438/getting-an-item-in-a-list">Getting an item in a list</a></p>
  
  <p><a href="https://stackoverflow.com/questions/15456845/getting-a-list-item-by-index">Getting a list item by index</a></p>
  
  <p><a href="https://stackoverflow.com/questions/7620648/get-array-item-based-on-index">Get array item based on index</a></p>