I have an issue returning some data in my application using the following C# code
[Queryable(AllowedQueryOptions = System.Web.Http.OData.Query.AllowedQueryOptions.Select)]
public override IQueryable<BrokerOutright> Get()
{
return db.BrokerOutrights.Where(b => b.BidBroker == User.Identity.Name
|| b.OfferBroker == User.Identity.Name)
.Select(x => new
{
x.Id,
x.Product,
x.Term,
(x.AskB=='dev')?x.AskB:null,
(x.AskB=='dev')?x.AVol:null
}
);
}
The above code threw me following exception
Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Linq.IQueryable
May I know a good explicit conversion?
Yes, because you're projecting to an anonymous type:
.Select(x => new
{
x.Id,
x.Product,
x.Term
})
It sounds like you want to use an object initializer for the BrokerOutright
type:
.Select(x => new BrokerOutright
{
Id = x.Id,
Product = x.Product,
Term = x.Term
})
Having said that, if db.BrokerOutrights
is already an IQueryable<BrokerOutright>
you may just want to ditch the Select
entirely:
return db.BrokerOutrights
.Where(b => b.BidBroker == User.Identity.Name ||
b.OfferBroker == User.Identity.Name);
If BrokerOutright
isn't a type that the LINQ provider knows about, you may need to perform the projection in LINQ to Objects instead using AsEnumerable()
- but then you'd have to return an IEnumerable<BrokerOutright>
instead of an IQueryable<BrokerOutright>
.
See more on this question at Stackoverflow