Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET MVC
Error   37  Cannot implicitly convert type 'System.Collections.Generic.List<SFM.Models.BL.EmployeeHistoryList>' to 'System.Collections.Generic.IEnumerable<SFM.Models.BL.EmployeeRegBL>'. An explicit conversion exists (are you missing a cast?)   C:\Documents and Settings\JavedAhmed\My Documents\Visual Studio 2010\Projects\SFM\SFM\Controllers\EmployeeHistoryController.cs  93  35  SFM
Posted 23-Jan-13 23:20pm
Comments
phil.o at 24-Jan-13 4:22am
   
Very interesting...
SureshDasari1986 at 24-Jan-13 4:40am
   
you can do it with same class
i.e
List of employeehistorylist to IEnumerable of employeehistorylist
 
and not with different classes
List of employeehistorylist to IEnumerable of employeeregbl
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You cannot do an explicit cast between the two types of collections you've mentioned in the error message.
For collection conversion, this[^] might help you.
  Permalink  
Comments
Orcun Iyigun at 24-Jan-13 5:04am
   
Nice link. My 5.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Exactly what it says, EmployeeHistoryList and EmployeeRegBL are two different classes.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

public class TestClass
    {
        public void TestMethod()
        {
            System.Collections.Generic.List<TestClass1> testClassList = new List<TestClass1>();
            testClassList.Add(new TestClass1() { TestVariable = 1, TestVariable1 = "TestValue" });
            testClassList.Add(new TestClass1() { TestVariable = 1, TestVariable1 = "TestValue" });
            testClassList.Add(new TestClass1() { TestVariable = 1, TestVariable1 = "TestValue" });
 
            // You can convert list to any supported type with the same class like this (i.e List<TestClass1> to IEnumerable<TestClass1>)
            System.Collections.Generic.IEnumerable<TestClass1> testClassEnumerable = testClassList.AsEnumerable();
 
            // You can't convert list to any supported type with other class like this (i.e List<TestClass1> to IEnumerable<TestClass2>)
            //System.Collections.Generic.IEnumerable<TestClass2> testClassEnumerable2 = testClassList.AsEnumerable();

            //you can Convert like this 'System.Collections.Generic.List<SFM.Models.BL.EmployeeHistoryList>' to 'System.Collections.Generic.IEnumerable<SFM.Models.BL.EmployeeHistoryList>'
            //you can Convert like this 'System.Collections.Generic.List<SFM.Models.BL.EmployeeRegBL>' to 'System.Collections.Generic.IEnumerable<SFM.Models.BL.EmployeeRegBL>'
            //you can't Convert like this 'System.Collections.Generic.List<SFM.Models.BL.EmployeeHistoryList>' to 'System.Collections.Generic.IEnumerable<SFM.Models.BL.EmployeeRegBL>'
        }
    }
 
    public class TestClass1
    {
        public int TestVariable { get; set; }
        public string TestVariable1 { get; set; }
    }
 
    public class TestClass2
    {
        public int TestVariable { get; set; }
        public string TestVariable1 { get; set; }
    }
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

EmployeeHistoryList and EmployeeRegBL are not matching types
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

You can use .toArray
 

System.Collections.Generic.List<int> list = new System.Collections.Generic.List<int>( );
list.Add( 1 );
System.Collections.Generic.IEnumerable<int> enumer = list.ToArray( );
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

Since 'an explicit conversion exists' you need to cast the variable to System.Collections.Generic.IEnumerable<sfm.models.bl.employeeregbl></sfm.models.bl.employeeregbl>
 
There is also IEnumerable.Cast<TResult>() extension method that will cast the type of all items in collection (if such cast is available).
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 575
1 Kornfeld Eliyahu Peter 409
2 Maciej Los 369
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,476
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,917


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 24 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100