如何将列表转换为IEnumerable?

时间:2017-01-06 23:37:01

标签: c# .net ienumerable

我有以下函数应该返回IEnumerable类型?如何将列表转换为IEnumerable?并返回一个空的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;
}

错误:

  

无法隐含转换类型'System.Collections.Generic.List&lt; ....&gt;至   System.Collections.Generic.IEnumerable&lt; .....&gt;。显式转换   存在(你是否错过演员表?)

error

3 个答案:

答案 0 :(得分:2)

您将返回两种不同的对象类型:

  • tblSoftwareImageTestPlan - 位于您的实体框架模型中
  • SoftwareImageTestPlan - 位于Qlasr架构模型中

因此,当您说明以下内容时:

return records;

它会抱怨records对象不属于SoftwareImageTestPlan类型。因此,您需要将records转换为可以通过List<SoftwareImageTestPlan>实现的新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();

然后您可以使用原始声明:

if (records == null)
    return new List<SoftwareImageTestPlan>();
else
    return records;

答案 1 :(得分:1)

问题不在于List<T>转换为IEnumerable<T>。因为List<T>实施IEnumerable<T>

您的问题是通用参数不同。您正在尝试将List<T1>转换为IEnumerable<T2>。其中:

  • T1为QlasrService.EntityFramework.tblSoftwareImageTestPlan
  • T2为QlasrService.Model.SchemaModels.LAP.SoftwareImageTestPlan

最简单的解决方案是映射(手动或automatic)。自动映射非常简单。添加Automapper nuget包。将此行放在应用程序启动的某处:

Mapper.Initialize(cfg => cfg.CreateMap<tblSoftwareImageTestPlan, SoftwareImageTestPlan>());

现在你的方法看起来像:

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

注意:在您的代码中,records不能具有null值,或者您NullReferenceExceptionToList() if..else。所以{{1}}块无论如何都是无用的。

答案 2 :(得分:1)

此处的问题不是您需要从List转换为IEnumerable

问题在于您是否尝试从List<tblSoftwareImageTestPlan>转换为IEnumerable<SoftwareImageTestPlan>

由于类型参数,这是两种完全不同的类型。

可能的解决方案:

  • 将退货类型更改为IEnumerable<tblSoftwareImageTestPlan>
  • 通过将SoftwareImageTestPlan投影到tblSoftwareImageTestPlan来将对象映射到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;
    }