locked
retrieve and pass data from EF table RRS feed

  • Question

  • Hi All.

    I would like to create method to retrieve whole data from EF table

    public static List<string> GetEmployee() { using (Model1 context = new Model1()) { List<string> employee = context.Employee.Select(a => a).ToList();

    return employee; } }

    But line with Select display error. What is wrong? How to fix it?

    Thanks.

    Wednesday, August 5, 2020 1:07 AM

Answers

  • You cannot use a list of string to return an list of employee. Take this example.

    Given an employee (click on the link to the left) get all employees

    We use

    using System.Collections.Generic;
    using System.Linq;
    using North.Contexts;
    using North.Models;
    
    namespace North.Classes
    {
        public class EmployeeTestOperations
        {
            public static List<Employees> AllEmployees()
            {
                using (var context = new NorthwindContext())
                {
                    return context.Employees.ToList();
                }
            }
        }
    }
    

    We get

    Now if we added this to the employee class above (in the link)

    public string Stringified => 
        $"{EmployeeID}, {FirstName}, {LastName}";

    Then change the read to this (partial select of columns)

    using System.Collections.Generic;
    using System.Linq;
    using North.Contexts;
    
    namespace North.Classes
    {
        public class EmployeeTestOperations
        {
            public static List<string> AllEmployees()
            {
                using (var context = new NorthwindContext())
                {
                    return context.Employees
                        .Select(employee => employee.Stringified)
                        .ToList();
                }
            }
        }
    }
    

    We get this

    What you have will never work, you can't select a list of employee and expect it to magically return a list of string.

    Does this make sense?


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Proposed as answer by Naomi N Thursday, August 6, 2020 9:45 PM
    • Marked as answer by Alberto PoblacionMVP Sunday, August 16, 2020 12:09 PM
    Wednesday, August 5, 2020 3:26 AM

All replies

  • Hi,

    You have List<string> should it not be List<Employee> ?

    public static List<Employees> GetEmployeeses()
    {
        using (var context = new Model1())
        {
            return context.Employee.ToList();
        }
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange



    Wednesday, August 5, 2020 1:09 AM
  • Hi Karen. Thank for reply.

    I tried like you suggested but got same error message:

    Cannot implicitly convert type 'System.Collections.Generic.List<Manage.Employee.DL.EF.Employee>' to 'System.Collections.Generic.List<string>

    If it is possible. Can you show how to retrieve whole table by the way like I tried?

    Thanks


    • Edited by zleug Wednesday, August 5, 2020 2:17 AM
    Wednesday, August 5, 2020 2:08 AM
  • You could not have possibly done what was suggested if the error message is thrown

    Cannot implicitly convert type 'System.Collections.Generic.List<Manage.Employee.DL.EF.Employee>' to 'System.Collections.Generic.List<string>

    The Employee class/type/object on the EF virtual object model can never be a string class/type/object that somehow you are trying to do on a cast, becuase they are two different classes/types. A class is a blueprint to an object instance, and one object can never be another object based on two different object blueprints their classes.

    The string class as opposed to the Employee class on EF's virtual object model.

    https://docs.microsoft.com/en-us/dotnet/api/system.string?view=netcore-3.1

    It's Object Oriented programming 101, and the principles  are the same Java or .NET.

    https://alfredjava.wordpress.com/2008/07/08/class-vs-object-vs-instance/

    If your are trying to return all Employee objects from the Employee database table, then the code below is correct. You are returning all the Employee objects in a list from the database on a Linq-2-Entities query.

    public static List<Employee> GetEmployeeses()
    {
        using (var context = new Model1())
        {
            return context.Employee.ToList();
        }
    }

    Wednesday, August 5, 2020 3:13 AM
  • You cannot use a list of string to return an list of employee. Take this example.

    Given an employee (click on the link to the left) get all employees

    We use

    using System.Collections.Generic;
    using System.Linq;
    using North.Contexts;
    using North.Models;
    
    namespace North.Classes
    {
        public class EmployeeTestOperations
        {
            public static List<Employees> AllEmployees()
            {
                using (var context = new NorthwindContext())
                {
                    return context.Employees.ToList();
                }
            }
        }
    }
    

    We get

    Now if we added this to the employee class above (in the link)

    public string Stringified => 
        $"{EmployeeID}, {FirstName}, {LastName}";

    Then change the read to this (partial select of columns)

    using System.Collections.Generic;
    using System.Linq;
    using North.Contexts;
    
    namespace North.Classes
    {
        public class EmployeeTestOperations
        {
            public static List<string> AllEmployees()
            {
                using (var context = new NorthwindContext())
                {
                    return context.Employees
                        .Select(employee => employee.Stringified)
                        .ToList();
                }
            }
        }
    }
    

    We get this

    What you have will never work, you can't select a list of employee and expect it to magically return a list of string.

    Does this make sense?


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Proposed as answer by Naomi N Thursday, August 6, 2020 9:45 PM
    • Marked as answer by Alberto PoblacionMVP Sunday, August 16, 2020 12:09 PM
    Wednesday, August 5, 2020 3:26 AM
  • Hi zleug,

    Thank you for posting here.

    Based on the error message, the above suggestions are correct.

    There are two places in your code that need to be modified: the reference type of employee and the return type of the method.

    Did you miss one of them?

    Best Regards,

    Timon


    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.

    Wednesday, August 5, 2020 5:30 AM
  • Hi Karen. Thanks for detail explanation.

    I will appreciate if you give me a link for good tutorial(s) about how to use List<string> and List<EntityTable>.

    Thanks.

    Wednesday, August 5, 2020 11:47 PM
  • Hi Karen. Thanks for detail explanation.

    I will appreciate if you give me a link for good tutorial(s) about how to use List<string> and List<EntityTable>.

    Thanks.

    Hello,

    I don't have any links or tutorials on this topic. The topic is simple (at least to me), if you want a List<string> in the Select you must specify what property you want, otherwise it's going to be a List<Employee> in your case.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, August 5, 2020 11:54 PM
  • Hi Karen. Thanks for detail explanation.

    I will appreciate if you give me a link for good tutorial(s) about how to use List<string> and List<EntityTable>.

    Thanks.

    What you have there is two strongly typed collections List<string> is based on the 'string 'type. It could be a List<int> that is  based on the 'int' type. You have a List<Employee> that is a custom class, a type,  derived from the Entity Framework, a persistence framework.

    Each one of the Lists mentioned is strong typed to a particular type. A class is a type. The only object that you can load into the  strong typed List<T> is the type it is strong typed for, remembering that a class is a blueprint for an  instanced object explained in a link in a previous post in this thread.

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/

    https://www.tutorialsteacher.com/csharp/csharp-data-types

    https://www.informit.com/articles/article.aspx?p=27578

    If the list defined as a List<int>, then you cannot load string data into a List<int> no more than you can load an Employee object into a List<string>.

    Thursday, August 6, 2020 7:26 PM