Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. Casting in LINQ

Casting in LINQ

Scheduled Pinned Locked Moved LINQ
helpcsharplinqquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kmuthuk
    wrote on last edited by
    #1

    Hi guys I have a problem in LINQ. My code looks like this:

    var EmpDetails = from emp in mymodel.Employees
    select new
    {
    Id = emp.Id,
    EmpName = emp.Name
    };

    When I assign the result to a list of employees like this:

    List lstEmployee = EmpDetails;

    I'm getting an error saying "Cannot convert implicitly from System.Collections.Generic.List to System.Collections.IEnumerable....". How could I avoid this error? Any help pls... Thanks Muthu.

    L E J 3 Replies Last reply
    0
    • K kmuthuk

      Hi guys I have a problem in LINQ. My code looks like this:

      var EmpDetails = from emp in mymodel.Employees
      select new
      {
      Id = emp.Id,
      EmpName = emp.Name
      };

      When I assign the result to a list of employees like this:

      List lstEmployee = EmpDetails;

      I'm getting an error saying "Cannot convert implicitly from System.Collections.Generic.List to System.Collections.IEnumerable....". How could I avoid this error? Any help pls... Thanks Muthu.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You are trying to cast an anonymous type to a list of defined types. Why not select all the employee data (so you have a defined type).

      var EmpDetails = from emp in myModel.Employees
      select emp;

      List<\employees\> list = empDetails.ToList();

      assuming you entity type is employees. If this isn't what you are looking for give me a shout :)

      At university studying Software Engineering - if i say this line to girls i find they won't talk to me Dan

      1 Reply Last reply
      0
      • K kmuthuk

        Hi guys I have a problem in LINQ. My code looks like this:

        var EmpDetails = from emp in mymodel.Employees
        select new
        {
        Id = emp.Id,
        EmpName = emp.Name
        };

        When I assign the result to a list of employees like this:

        List lstEmployee = EmpDetails;

        I'm getting an error saying "Cannot convert implicitly from System.Collections.Generic.List to System.Collections.IEnumerable....". How could I avoid this error? Any help pls... Thanks Muthu.

        E Offline
        E Offline
        Eslam Afifi
        wrote on last edited by
        #3

        Or maybe

        var lstEmployee = EmpDetails.ToList();

        Eslam Afifi

        1 Reply Last reply
        0
        • K kmuthuk

          Hi guys I have a problem in LINQ. My code looks like this:

          var EmpDetails = from emp in mymodel.Employees
          select new
          {
          Id = emp.Id,
          EmpName = emp.Name
          };

          When I assign the result to a list of employees like this:

          List lstEmployee = EmpDetails;

          I'm getting an error saying "Cannot convert implicitly from System.Collections.Generic.List to System.Collections.IEnumerable....". How could I avoid this error? Any help pls... Thanks Muthu.

          J Offline
          J Offline
          Jon Rista
          wrote on last edited by
          #4

          List is a generic type. You can't just have List, you have to have to specify a type for T: List<T>. You don't actually have a known type because you created anonymous types in your query, so you had to use a var type variable to hold the results. If you want to be able to create a List<T>, you need a concrete type:

          class EmpDetail
          {
          public int Id { get; set; }
          public string EmpName { get; set; }
          }

          // ...

          IEnumerable<EmpDetail> empDetails = from emp in mymodel.Employees select new EmpDetail { Id = emp.Id, EmpName = emp.Name };
          List<EmpDetail> empDetailsList = empDetails.ToList();

          K 1 Reply Last reply
          0
          • J Jon Rista

            List is a generic type. You can't just have List, you have to have to specify a type for T: List<T>. You don't actually have a known type because you created anonymous types in your query, so you had to use a var type variable to hold the results. If you want to be able to create a List<T>, you need a concrete type:

            class EmpDetail
            {
            public int Id { get; set; }
            public string EmpName { get; set; }
            }

            // ...

            IEnumerable<EmpDetail> empDetails = from emp in mymodel.Employees select new EmpDetail { Id = emp.Id, EmpName = emp.Name };
            List<EmpDetail> empDetailsList = empDetails.ToList();

            K Offline
            K Offline
            kmuthuk
            wrote on last edited by
            #5

            This works. :thumbsup: You guys are awesome!!! But I had to create another class for Employee as you mentioned, this is almost replica of mymodel.Employee. There is not much difference between them other than inheriting from INotifyPropertyChanging, INotifyPropertyChanged. Is that the one preventing from converting it to list using ToList()? Any thoughts? Thanks a lot. Muthu.

            J 1 Reply Last reply
            0
            • K kmuthuk

              This works. :thumbsup: You guys are awesome!!! But I had to create another class for Employee as you mentioned, this is almost replica of mymodel.Employee. There is not much difference between them other than inheriting from INotifyPropertyChanging, INotifyPropertyChanged. Is that the one preventing from converting it to list using ToList()? Any thoughts? Thanks a lot. Muthu.

              J Offline
              J Offline
              Jon Rista
              wrote on last edited by
              #6

              If you have an existing Employee entity...just use it. You don't have to create an intermediate class if you don't want one. Type initializers will work on any class that has public get/set properties. Or, if you require an abstracted creation process (such as through a factory, you can call your factory method directly from the LINQ statement):

              // Employee entity
              class Employee
              {
              public int ID { get; set; }
              public string Name { get; set; }
              // ...
              }

              // Employee factory
              static class EmployeeFactory
              {
              static Employee Construct(int id, string first, string last //, ...)
              {
              Employee emp = new Employee();
              emp.ID = id;
              emp.Name = last + ", " + first;

                  return emp;
              }
              

              }

              // Query
              IEnumerable employees = from e in db.Employee
              where e.DateHired > DateTime.Now.AddMonths(-6)
              select EmployeeFactory.Construct(e.ID, e.First, e.Last);

              IList employeeList = employees.ToList();

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups