Reputation: 2439
I have this linq query which will select all the records in my table InventoryLocation. however it gives me an error... I dunno what is the problem... I'm really new to this so bear with me...
Error
Cannot implicitly convert type System.Linq.IQueryable to System Collections.Generic.IEnumerable
thanks in advance..
public IEnumerable<InventoryLocationModel> GetInventoryLocations()
{
IEnumerable<InventoryLocationModel> results = null;
using (DataContext ctx = new DataContext())
{
results = from a in ctx.InventoryLocations select a;
}
return results;
}
Upvotes: 3
Views: 5095
Reputation: 144112
This will get what you want:
results = (from a in ctx.InventoryLocations select a).AsEnumerable();
This ensures that you still take advantage of LINQ's deferred execution.
Upvotes: 2
Reputation: 1800
The main issue is that you aren't actually executing the query so you are left with just the IQueryable instead of actual results, thus you are getting the error.
public IEnumerable<InventoryLocationModel> GetInventoryLocations()
{
IEnumerable<InventoryLocationModel> results = null;
using (DataContext ctx = new DataContext())
{
results = (from a in ctx.InventoryLocations select a).ToList();
}
return results;
}
You can also simplify your query. All you really need for what you are doing is
public IEnumerable<InventoryLocationModel> GetInventoryLocations()
{
using (DataContext ctx = new DataContext())
{
return ctx.InventoryLocations.ToList();
}
}
Upvotes: 2