Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert list to IEnumerable?

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?)

error

like image 572
Ritz Avatar asked Jan 06 '17 23:01

Ritz


3 Answers

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:

  • T1 is QlasrService.EntityFramework.tblSoftwareImageTestPlan
  • T2 is 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.

like image 141
Sergey Berezovskiy Avatar answered Nov 10 '22 19:11

Sergey Berezovskiy


You're returning two different object types:

  • tblSoftwareImageTestPlan - Which resides in your Entity Framework model
  • SoftwareImageTestPlan - Which resides in your Qlasr Schema Models

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;
like image 44
Darren Avatar answered Nov 10 '22 20:11

Darren


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:

  • Change the return type to 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;
    }
    
like image 34
Kenneth Avatar answered Nov 10 '22 20:11

Kenneth