[Solved] ASP.Net Error: cannot implicitly convert type List<> to List

sureshMGR
 
on Feb 16, 2021 11:28 PM
1074 Views

 Hi friends,

when i try to return list i will got below error

cannot implicitly convert type System.Collection.generic.list<<anonymous type:int startYear>> to System.Collection.generic.list<project.model.RefTbl>

please help

 
public List<RefTbl> GetRefYears()
{
    var items = db.RefTbl.Where(x => x.Active == true).OrderByDescending(x => x.StartDate.Value.Year)
    .Select(x => new
    {
        startYear = x.StartDate.Value.Year
    }).Distinct().ToList();
    return items;
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Feb 16, 2021 11:30 PM

Hi sureshMGR,

Change with below code.

public List<RefTbl> GetRefYears()
{
    var items = db.RefTbl.Where(x => x.Active == true).OrderByDescending(x => x.StartDate.Value.Year)
    .Select(x => new RefTbl
    {
        startYear = x.StartDate.Value.Year
    }).Distinct().ToList();

    return items;
}