Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I don't understand why I'm getting the following error:

XML
Cannot implicitly convert type 'System.Collections.Generic.List<Contract>' to 'System.Collections.Generic.List<string>'



public class Service : IService
{
    public List<string> GetContract(string Contract)
    {
        Contract_GenDataContext db = new Contract_GenDataContext();
        var matchingContract = from con in db.Contracts
                               where con.Contract1.Contains(Contract)
                               select con;
        return matchingContract.ToList();
    }
}
Posted

Don't you think the reason is obvious and clear! You are trying to return Generic List of type contract where the return types defined is List of type string?

Change the return type to List<Contract> OR convert List<Contract> to List<String> explicitly.

XML
public List<Contract> GetContract(string Contract)
{
    Contract_GenDataContext db = new Contract_GenDataContext();
    var matchingContract = from con in db.Contracts
                           where con.Contract1.Contains(Contract)
                           select con;
    return matchingContract.ToList();
}
 
Share this answer
 
v2
Hi,

The collection you are returning here i.e. matchingContract is a type of Contract and db.Contracts is a collection of Contract.

But, as your method return type is of type List<string></string> it is throwing Conversion Error. The message shown by Visual Studio is proper and you can easily get the issue.
 
Share this answer
 

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