如何将列表转换为 IEnumerable?

how to convert list to IEnumerable?

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

错误:

Cannot implicty convert type 'System.Collections.Generic.List<....> to System.Collections.Generic.IEnumerable<.....>.An explicit conversion exists(are you missing a cast?)

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

  • tblSoftwareImageTestPlan - 位于您的 Entity Framework 模型中
  • 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;

问题不在于将 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 值,要么您将在 ToList() 调用时具有 NullReferenceException。所以 if..else 块无论如何都没用。

这里的问题不是您需要将 List 转换为 IEnumerable

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

这是两种完全不同的类型,因为类型参数。

可能的解决方案:

  • 将 return 类型更改为 IEnumerable<tblSoftwareImageTestPlan>
  • 通过将 tblSoftwareImageTestPlan 投影到 SoftwareImageTestPlan:

    ,将对象映射到 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;
    }