Reputation: 3
I have this model in C#:
public class interfaceModel
{
public string interfacename { get; set; }
public long id { get; set; }
}
and write this code:
public static List<interfaceModel> CompanyInterfaceFetch()
{
var database = new DataBaseDataContext();
var companyinterface = new CompanyInterface();
var interfacelist =
database.CompanyInterfaces.Select(x => new interfaceModel { interfacename = x.InterfaceName, id=x.id});
return interfacelist.ToList();
}
// database model
public class interfaceModel
{
public string interfacename { get; set; }
public long id { get; set; }
}
Now I write code to call that method:
public static List<interfaceModel> interfacefetch()
{
return Database.CompanyInterfaceFetch();
}
but on this line:
return Database.CompanyInterfaceFetch();
I get this error:
Error CS0029: Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.List
How can I solve that problem? Thanks.
Upvotes: 0
Views: 3163
Reputation: 3948
Since you have two types spread out in different namespaces, you cannot convert from one type to another.
To me it looks alright, from design perspective!
One of the ways to solve this problem would be to employ Automapper!
Upvotes: -1
Reputation: 1948
Your Error is clear:
Error CS0029 Cannot implicitly convert
type
'System.Collections.Generic.List<Web.Controllers.Database.interfaceModel>'
to
'System.Collections.Generic.List<Web.Models.interfaceModel>'
You have two types of the same name in these two locations.
If they have the same fields then you should remove one of them. If they are different then you need to fix the imports in your code so that CompanyInterfaceFetch()
and interfacefetch()
refer to the same type. If necessary drop the imports and specify the fully qualified class name (like in the exception message).
Upvotes: 0
Reputation: 156918
You have two separate classes with the same name. Judging the naming, this is the location of both:
The two types, although they have the same name and even might have the exact same fields, are not exchangeable. Remove either of them and compile again to check the errors to see what adjustments you have to make.
Upvotes: 3