none
Newbie here need help with Entity and Table! RRS feed

  • Question

  • Sorry to bother... I'm learning c# and found a problem that I don't know how to resolve... its about Entity connection to a local table... now I can loop using FOREACH... by curiosity I tried to make it using FOR instead... I get an ugly error... I can't refer to the item or member as Tabla[INDEX] as I usually do with Lists NOR can use the ElementAt(INDEX)... would you please tell me how to point to an specific item on my Table? thank for your help!
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace LocalDbExample
    {
        public partial class Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
                conexionACME miConexion = new conexionACME();
    
                var Tabla = miConexion.Customers;
    
                for (int i = 0; i < Tabla.Count<Customer>(); i++)
                {
                    Customer myCS = Tabla.ElementAt<Customer>(i); //---------------------->> ERROR!!!!
                    
                    resultLabel.Text += $"{myCS.CustomerID}<br>";
    
                }
    
            }
        }
    }


    • Moved by CoolDadTx Friday, September 15, 2017 9:45 PM EF related
    Friday, September 15, 2017 9:22 PM

All replies

  • Please provide the Exception you get.

    My guess is, that since the EntityFramework queries on execution, you should use a .ToList() before ElementAt. SO that the query gets executed and an index is available. consider:

    protected void Page_Load(object sender, EventArgs e)
    {
        conexionACME miConexion = new conexionACME();
    
        var Tabla = miConexion.Customers.ToList();
    
        for (int i = 0; i < Tabla.Count; i++)
        {
            Customer myCS = Tabla[i];
            resultLabel.Text += $"{myCS.CustomerID}<br>";
        }
    }

    also helpful would be the code behind the conexionACME.Customers (if you do wrap the EF connector, if you dont, just do that)

    i also cant resist: consider writing your code in english, it more broadly understood, also since the .NET is english, it's easier readable.


    Please be so kind to close your Threads when you found an answer, these Threads should help everyone with similar issues.
    You can close a Thread via the"Mark as Answer" link below posts. You can mark your own posts as answers if you were not helped out but found a solution, in such a case, please provide the answer.
    Happy coding
    PS: I assure everyone that I did not ever had the desire to offend anyone.




    • Edited by MDeero Friday, September 15, 2017 9:37 PM
    Friday, September 15, 2017 9:29 PM
  • Hi PerezGuillermo8,

    Thank you for posting the problem here.

    From your code and description, I suppose that you would like to read some data from a DbSet type object by using Entity Framework.

    For instance, I made a demo which can read the data from a DbSet type object, as follows.

    namespace App19CodeFirstEF
    {
        class Program
        {
            static void Main(string[] args)
            {
                string result = "";
                PersonDbContext db = new PersonDbContext();
                var Tabla = db.Persons;
                for (int i = 0; i < Tabla.Count<Person>(); i++)
                {
                    Person person =Tabla.ToList()[i];
                    result += person.Name + "*";
                }
            }
        }
        public class Person
        {
            public int PersonId { get; set; }
            public string Name { set; get; }
            public string Email { set; get; }
            public int Age { set; get; }
        }
        public class PersonDbContext : DbContext
        {
            public DbSet<Person> Persons { set; get; }
        }
    }
    

    Here is the result :

    You could refer to my code to modify your code, as follows.

    var Tabla = miConexion.Customers;
       for (int i = 0; i < Tabla.Count<Customer>(); i++)
       {
           Customer myCS =Tabla.ToList()[i];
           resultLabel.Text += $"{myCS.CustomerID}<br>";
    }
    

    If you have any other questions about my reply, please contact me freely.

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, September 19, 2017 11:07 AM
  • Hi PerezGuillermo8,

    Has your problem been resolved? We are waiting for your feedback. If a reply resolved your problem, please be so kind to close your Threads when you found an answer, these Threads should help everyone with similar issues.

    You can close a Thread via the" Mark as Answer" link below posts. You can mark your own posts as answers if you were not helped out but found a solution, in such a case, please provide the answer.

    If you have any other problem, you could create a new thread.

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Bob Ding Monday, September 25, 2017 8:04 AM
    Monday, September 25, 2017 8:04 AM