I am trying to return an entity framework list in a C# class application using ToList, I am Just not getting it.

Appdev 0 Reputation points
2023-05-03T20:46:04.8966667+00:00

Hi All,

I am trying to turn some function I created in an mvc application and place them into a class Library a the computer is not having it... let me demonstrate.

This is the Error I get

Severity Code Description Project File Line Suppression State

Error CS0029 Cannot implicitly convert type 'System.Collections.Generic.List<DSDB_LIBRARY.Models.DSDB_STORE_TBL>' to 'System.Collections.Generic.List<string>' DSDB_LIBRARY C:\APP_DEV\DSDB_LIBRARY\DSDB_LIBRARY\DBDS_ClassV1.cs 178 Active

I am not sure what this error means as it does not provide any information on how one would go about fixing it.

I have another program where I made it to a list but the EF linq statement is different I will post the source codes at the bottom show that the ToList will work but it required a different ling statement but I don't get why

My MVC function looks like this , This sends in a ID and returns all rows that have the code in the DOC_SOURCE__DIS field. So If I have 20 rows it returns that the view

        [HttpPost]

        public ActionResult Gate(string DocCode)
        {
            DSDBEntities DB_Entities = new DSDBEntities();

            var DocList = DB_Entities.DSDB_STORE_TBL.Where(d => d.DOC_SOURCE_DISC == DocCode).ToList();

            return View(DocList);
          


        }

Here is my attempt to make it work in the class library. I would like the same thing to happen in this calls library if possible The User sends in the code DocCode the system gets the information from the database allowing me to pass it back as something I can use in a mvc program or a webforms program or what ever I may need. I could not find anything online that would help some hopefully some one here can help. Thanks so very much.

        public List<string> GET_DOCUMENTS_BY_DocCode(string DocCode)
        {

            DSDB_CLASS_Entities db = new DSDB_CLASS_Entities();


            var DocList = db.DSDB_STORE_TBL.Where(d => d.DOC_SOURCE_DISC == DocCode).ToList();

            return DocList;

        }

Error


THe Error is :

Severity	Code	Description	Project	File	Line	Suppression State
Error	CS0029	Cannot implicitly convert type 'System.Collections.Generic.List<DSDB_LIBRARY.Models.DSDB_STORE_TBL>' to 'System.Collections.Generic.List<string>'	DSDB_LIBRARY	C:\APP_DEV\DSDB_LIBRARY\DSDB_LIBRARY\DBDS_ClassV1.cs	178	Active

This tolist worked, its stuff like this where there is an error that blows my mind. So any help that anyone can provide I would be grateful as I am trying to understand what is going on here.

        public List<string> GetStaffEmail(string PositID)
        {
            //CONVERTING THE POSIT BECAUSE IT WAS ORGINALLY A STRING IN THE OTHER TABLE
            var StaffEmailList = FMSdb.VUE_RVRS_EMPLOYEE_POSIT.Where(p => p.Manager_ID == Convert.ToInt16(PositID)).Select(p => p.Email).ToList();

            return StaffEmailList;

         }
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,919 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,507 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,193 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,011 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 66,706 Reputation points
    2023-05-03T21:32:28.08+00:00

    with linq, ToList() return the type of the collection unless mapped to a new type with a select(). so

    var DocList = db.DSDB_STORE_TBL.Where(d => d.DOC_SOURCE_DISC == DocCode).ToList();
    

    DocList will a List of type of db.DSDB_STORE_TBL (DSDB_LIBRARY.Models.DSDB_STORE_TBL) or

    List<DSDB_LIBRARY.Models.DSDB_STORE_TBL> DocList = db.DSDB_STORE_TBL.Where(d => d.DOC_SOURCE_DISC == DocCode).ToList();
    

    in your email example you mapped each row to a single string value via the Select()


  2. Lan Huang-MSFT 29,751 Reputation points Microsoft Vendor
    2023-05-04T06:57:41.24+00:00

    Hi @Appdev,

    A .Where() method will filter the rows returned.

    A .Select() method will filter the columns returned.

    var DocList = db.DSDB_STORE_TBL.Where(d => d.DOC_SOURCE_DISC == DocCode).ToList();

    DocList returns multiple Docs, you will need List<DSDB_STORE_TBL>(List<T>).

     public class DSDB_STORE_TBL
    {
        public string DocCode {get; set;}
        public int ... {get; set;}
    .....
    }
    
     public List<DSDB_STORE_TBL> GET_DOCUMENTS_BY_DocCode(string DocCode)
            {
    
                DSDB_CLASS_Entities db = new DSDB_CLASS_Entities();
    
    
                var DocList = db.DSDB_STORE_TBL.Where(d => d.DOC_SOURCE_DISC == DocCode).ToList();
    
                return DocList;
    
            }
    

    when you put Select(p => p.Email) after the Where() now it will return Collection of string not Collection of that type.That's why the first reports an error and the second doesn't.

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.