0

How do I convert from IQueryable to IEnumerable? I looked at another post Get from IQueryable to IEnumerable but it's not generic enough to reuse? Can anyone provides a generic reusable way to do this?

public class SoftwareProductRepository
{
    BitDatabaseEntities bitDB = new BitDatabaseEntities();
    public IEnumerable<BuildMetrics> GetBuildMetrics()
    {

        var list = (from r in bitDB.bit_sanity_results select r);

        return list;
    }
}

Error message:

Cannot implictly converty type 'System.Linq.IQueryable<Dashboard.EntityFramework.bit_sanity_results>' to 'System.Collections.IEnumerable<Dashboard.Model.ApiModels.BuildMetrics>'.An explicit conversion exists(are you missing a cast?)

Community
  • 1
  • 1
user3508811
  • 847
  • 4
  • 19
  • 43

2 Answers2

3

Try converting queryable to enumerable with this:

(from r in bitDB.bit_sanity_results select r).AsEnumerable();

This will give you an enumerable collection but I think you may have to generate BuildMetrics objects from bit_sanity_results that are returned by this. Like:

(from r in bitDB.bit_sanity_results select new BuildMetrics{
       <some BuildMetrics property>=r.<some bit_sanity_results property>
        }).AsEnumerable();

Example

public class ReturnClass
{
    public string SSId{get;set;}
    public string Name{get;set;}
}

Table from which we select:

table values_store
(
    ss_id varchar(50),
    user_name varchar(250)
)

public class ReturnValuesRepository
{
    public IEnumerable<ReturnClass> GetReturnClasses()
    {   
        //As commented by @Enigmativity if your `BitDatabaseEntities` is `IDisposable` (sure if it inherits from `DbContext`) you should wrap it in a `using` block
        using(var db = new ValuesEntities())
        {
            var list = (from r in db.values_store select new 
                   ReturnClass
                     {
                         SSId=r.ss_id,
                         Name=r.user_name
                    }).AsEnumerable();

            return list;
        }
    }
}
TheVillageIdiot
  • 40,053
  • 20
  • 133
  • 188
1

The conversion in this case should actually be implicit as IQueryable<T> implements IEnumerable<T>.

Your problem here is a type mismatch, as you are getting IQueryable<bit_sanity_results> but are trying to return IEnumerable<BuildMetrics>.

You'd need to project the IQueryable<bit_sanity_results> to IEnumerable<BuildMetrics>.

Something like this:

public class SoftwareProductRepository
{
    BitDatabaseEntities bitDB = new BitDatabaseEntities();
    public IEnumerable<BuildMetrics> GetBuildMetrics()
    {
        return bitDB.bit_sanity_results.Select(r => new BuildMetrics(...));
    }
}
Zaid Masud
  • 13,225
  • 9
  • 67
  • 88
  • this throws an error "cannot implicity convert type 'System.Collections.Generic.List'.An explicit conversion exists – user3508811 Feb 28 '17 at 01:37
  • 1
    @user3508811 so you're not using exactly this example. This is the right way of doing what you want. If you are doing something like `List myList = GetBuildMetrics()` it's going to throw the error you said. – Alisson Reinaldo Silva Feb 28 '17 at 01:55