I have the following function in which should return an IEnumerable type?how do I convert list to IEnumerable? and return an empty IEnumerable?
public IEnumerable<SoftwareImageTestPlan> GetAssignedTestPlansForSPSI(int SoftwareProductID, int SoftwareImageID)
{
var records = _entities.tblSoftwareImageTestPlans
.Where(x => x.SoftwareProductID == SoftwareProductID && x.SoftwareImageID == SoftwareImageID)
.ToList();
if (records == null)
return new List<SoftwareImageTestPlan>();
else
return records;
}
Error:
Cannot implicty convert type 'System.Collections.Generic.List<....> to System.Collections.Generic.IEnumerable<.....>.An explicit conversion exists(are you missing a cast?)
You're returning two different object types:
Therefore when you state the following:
return records;
It will complain that the records
object is not of type SoftwareImageTestPlan
. Therefore you need to convert records
into a new List<SoftwareImageTestPlan>
which you can achieve via a LINQ projection
.
var records = (from entities in _entities.tblSoftwareImageTestPlans
where entities.SoftwareProductID equals SoftwareProductID && entities.SoftwareImageID == SoftwareImageId
select new SoftwareImageTestPlan
{
SoftwareProductID = entities.SoftwareProductID,
SoftwareImageID = entities.SoftwareImageID
}).ToList();
You can then use your original statement:
if (records == null)
return new List<SoftwareImageTestPlan>();
else
return records;
Problem is not in convertation of List<T>
to IEnumerable<T>
. Becuase List<T>
implements IEnumerable<T>
.
Your problem is that generic parameters are different. You are trying to convert List<T1>
to IEnumerable<T2>
. Where:
QlasrService.EntityFramework.tblSoftwareImageTestPlan
QlasrService.Model.SchemaModels.LAP.SoftwareImageTestPlan
Simplest solution will be mapping (either manual or automatic ). Automatic mapping is very easy. Add Automapper nuget package. Place this line somewhere on application start:
Mapper.Initialize(cfg => cfg.CreateMap<tblSoftwareImageTestPlan, SoftwareImageTestPlan>());
And now your method will look like:
public IEnumerable<SoftwareImageTestPlan> GetAssignedTestPlansForSPSI(
int SoftwareProductID, int SoftwareImageID)
{
var testPlans = from tp in _entities.tblSoftwareImageTestPlans
where tp.SoftwareProductID == SoftwareProductID && tp.SoftwareImageID == SoftwareImageID
select tp;
return Mapper.Map<IEnumerable<SoftwareImageTestPlan>>(testPlans);
}
NOTE: In your code either records
cannot have null
value, or you will have NullReferenceException
at ToList()
call. So if..else
block is useless anyway.
The problem here isn't that you need to convert from List
to IEnumerable
.
The problem is that you're trying to convert from List<tblSoftwareImageTestPlan>
to IEnumerable<SoftwareImageTestPlan>
Those are two completely different types, because of the type argument.
Possible solutions:
IEnumerable<tblSoftwareImageTestPlan>
Map the objects to SoftwareImageTestPlan
by projecting the tblSoftwareImageTestPlan
to a SoftwareImageTestPlan
:
public IEnumerable<SoftwareImageTestPlan> GetAssignedTestPlansForSPSI(int softwareProductID, int SoftwareImageID) { var records = _entities.tblSoftwareImageTestPlans .Where(x => x.SoftwareProductID == SoftwareProductID && x.SoftwareImageID == SoftwareImageID) .Select(x => new SoftwareTestPlan { Id = Id, // example ... do more mapping here }) .ToList(); if (records == null) return new List<SoftwareImageTestPlan>(); else return records; }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.