Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
XML
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
Comments
phil.o 24-Jan-13 4:22am    
Very interesting...
Suresh Dasari's 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

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.
 
Share this answer
 
Comments
Orcun Iyigun 24-Jan-13 5:04am    
Nice link. My 5.
Exactly what it says, EmployeeHistoryList and EmployeeRegBL are two different classes.
 
Share this answer
 
C#
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; }
    }
 
Share this answer
 
v2
EmployeeHistoryList and EmployeeRegBL are not matching types
 
Share this answer
 
You can use .toArray


C#
System.Collections.Generic.List<int> list = new System.Collections.Generic.List<int>( );
list.Add( 1 );
System.Collections.Generic.IEnumerable<int> enumer = list.ToArray( );
 
Share this answer
 
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).
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900