locked
cannot convert from System.Collections.Generic.List<> to System.Collections.Generic.IEnumerable<> RRS feed

  • Question

  • User2105881209 posted
    public class SystemAccessList
    {
         public SystemAccess SystemAccess { get; set; }
    
         public List<SystemAccess> SystemAccessList { get; set; }
    
         SystemDbContext db;
    
     public void setDbContext(PALMSConfigDbContext _db)
        {
            db = _db;
        }
    
      public SystemAccessList GetAccessList(SystemAccessList systemAccessList)
      {
    
        var qresult = db.tbl_SystemAccessList
                .GroupBy(g => g.ClassID)
                .AsEnumerable().Select(c =>
                {
                    var rr = new ResultRow { Class = c.Key };
                    foreach (var r in db.tbl_SystemAccessList.GroupBy(gg => gg.StudentID))
                    {
                        rr.Student[r.Key] = "N";
                    }
                    foreach (var r in c.Where(w => w.ClassID == c.Key))
                    {
                        rr.Student[r.StudentID] = "Y";
                    }
                    return rr;
                }).ToList();
    
        systemAccessList.SystemAccessList.AddRange(qresult);
        return systemAccessList;
    }
    
    class ResultRow
    {
        public ResultRow()
        {
            Student = new Dictionary<string, string>();
        }
    
        public string Class { get; set; }
    
        public IDictionary<string, string> Student { get; set; }
    
        public string GetHeader()
        {
            return Student.Aggregate("Class", (current, s) => current + "|" + s.Key);
        }
    
        public string GetSolidRow()
        {
            return Student.Aggregate(Class, (current, s) => current + "|" + s.Value);
        }
        }
    
      public class SystemAccess
       {
           [Key]
           public int ID { get; set; }
           public string ClassID { get; set; }
           public string StudentID { get; set; }
       }

    Thursday, June 16, 2016 7:27 AM

Answers

  • User-271186128 posted

    Hi ancmvc,

    According to your code and description, I create a sample using the following code, it works well on my side:

            public ActionResult Index10()
            {
                var query2 = context.Employees.GroupBy(g => g.EmployeeID)
                    .AsEnumerable().Select(c =>
                {
                    var ee = new MyEmployee { ID = c.Key };
                    foreach (var rr in c.Where(d => d.EmployeeID == c.Key))
                    {
                        ee.Name = rr.FirstName;
                    }
                    return ee;
                }).ToList();
                List<MyEmployee> eeList = new List<MyEmployee>();
                eeList.AddRange(query2);
                return View(query2);
            }

    So, I suggest you post the details error message. And, you can set a break point to check which line give this error.

      public SystemAccessList GetAccessList(SystemAccessList systemAccessList)
      {
    
        var qresult = db.tbl_SystemAccessList
                .GroupBy(g => g.ClassID)
                .AsEnumerable().Select(c =>
                {
                    var rr = new ResultRow { Class = c.Key };

    foreach (var r in db.tbl_SystemAccessList.GroupBy(gg => gg.StudentID)) { rr.Student[r.Key] = "N"; } foreach (var r in c.Where(w => w.ClassID == c.Key)) { rr.Student[r.StudentID] = "Y"; } return rr; }).ToList(); systemAccessList.SystemAccessList.AddRange(qresult); return systemAccessList; }

    Besides, I suggest you check above code, according to the SystemAccessList class, we can see the type of SystemAccessList property is List<SystemAccess>. But, from your code, it will return a List of ResultRow

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 17, 2016 7:23 AM

All replies

  • User-1034726716 posted

    What's the type of your SystemAccessList? The error is very much self explanatory and that means you cannot pass an object of type System.Collections.Generic.List<> to type to System.Collections.Generic.IEnumerable<>. You need to modify your return object that would match with the object that it expects.

    Thursday, June 16, 2016 5:27 PM
  • User303363814 posted

    Delete the

    .AsEnumerable()

    and

    .ToList()

    run your code.  Do you get an error?  Which line gives the error?  What is the exact error?  If the error is related to types then give us the exact type of the variables involved, if some of the variables are of user-defined classes then show us the class details.  Show the signature of any methods being called in the statement which gives the error.

    Thursday, June 16, 2016 9:40 PM
  • User2105881209 posted

    i just edit to include my full code. Can please guide on how to fix it? thanks

    Friday, June 17, 2016 3:34 AM
  • User2105881209 posted

    If i remove the .AsEnumerable i will get "a Lambda Expression with a statement body cannot be converted to an expression tree"in the .Select().

    Friday, June 17, 2016 4:49 AM
  • User-271186128 posted

    Hi ancmvc,

    According to your code and description, I create a sample using the following code, it works well on my side:

            public ActionResult Index10()
            {
                var query2 = context.Employees.GroupBy(g => g.EmployeeID)
                    .AsEnumerable().Select(c =>
                {
                    var ee = new MyEmployee { ID = c.Key };
                    foreach (var rr in c.Where(d => d.EmployeeID == c.Key))
                    {
                        ee.Name = rr.FirstName;
                    }
                    return ee;
                }).ToList();
                List<MyEmployee> eeList = new List<MyEmployee>();
                eeList.AddRange(query2);
                return View(query2);
            }

    So, I suggest you post the details error message. And, you can set a break point to check which line give this error.

      public SystemAccessList GetAccessList(SystemAccessList systemAccessList)
      {
    
        var qresult = db.tbl_SystemAccessList
                .GroupBy(g => g.ClassID)
                .AsEnumerable().Select(c =>
                {
                    var rr = new ResultRow { Class = c.Key };

    foreach (var r in db.tbl_SystemAccessList.GroupBy(gg => gg.StudentID)) { rr.Student[r.Key] = "N"; } foreach (var r in c.Where(w => w.ClassID == c.Key)) { rr.Student[r.StudentID] = "Y"; } return rr; }).ToList(); systemAccessList.SystemAccessList.AddRange(qresult); return systemAccessList; }

    Besides, I suggest you check above code, according to the SystemAccessList class, we can see the type of SystemAccessList property is List<SystemAccess>. But, from your code, it will return a List of ResultRow

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 17, 2016 7:23 AM
  • User303363814 posted

    Sorry, I missed that you are using Linq2Sql.

    Which line gives the error?  What is the exact error?  If the error is related to types then give us the exact type of the variables involved, if some of the variables are of user-defined classes then show us the class details.  Show the signature of any methods being called in the statement which gives the error.

    Friday, June 17, 2016 8:04 AM