Ritz
Ritz

Reputation: 1253

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

Upvotes: 13

Views: 45840

Answers (3)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236188

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.

Upvotes: 12

Darren
Darren

Reputation: 70718

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;

Upvotes: 5

Kenneth
Kenneth

Reputation: 28737

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;
    }
    

Upvotes: 2

Related Questions