1

How do I correctly return a list of "CarTypes" objects (from the second method), where the TyreID that is passed in, is not the primary key of the CarType class - so for example, I want to return a list of all CarTypes, where the TyreID is 5:

// GET api/CarTypes
public IEnumerable<CarTypes> GetCarTypes()
{
    return db.CarTypes.AsEnumerable();  //This works fineCar
}

// GET api/CarTypes/5
public IEnumerable<CarTypes> GetCarTypes(long id)
{
    CarTypes cartypes = db.CarTypes.Select(t => t.TyreID == id).AsEnumerable();
    if (roomtypes == null)
    {
        throw new HttpResponseException(Request
            .CreateResponse(HttpStatusCode.NotFound));
    }

    return cartypes;
}

It currently shows the error:

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'MvcApplication4.Models.CarTypes'. An explicit conversion exists (are you missing a cast?)

And does it matter if I use Select/SelectMany/Where in the query?

3 Answers 3

9

Firstly you need to use Where instead of Select; secondly you don't need to use AsEnumerable() after you've changed it to Where but you might have to call ToList() so that the Linq2Sql/EntityFramework executes the query before returning the values to the view.

 // GET api/CarTypes/5
    public IEnumerable<CarTypes> GetCarTypes(long id)
    {
        var cartypes = db.CarTypes.Where(t => t.TyreID == id).ToList();
        if (cartypes == null || !cartypes.Any())
        {
            throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
        }

        return cartypes;
    }

I've also added in an additional check after the query has executed but you might not need this depending on how you want to handle an empty collection.

Sign up to request clarification or add additional context in comments.

4 Comments

Hi @WestDiscGolf - VS (2012 RC) shows an error on the db.CarTypes... Cannot implicitly convert type 'System.Collections.Generic.List<MvcApplication4.Models.CarTypes>' to 'MvcApplication4.Models.cartypes
Hi @WestDiscGolf, it might be too forum to comment on, but have a question on the above implementation, if you look at the API, it returns a list but we have IEnumerable as the return type for the method. The above implementation will succeed.. No doubt about it. My questions are, 1) is it fine to use both interchangeably 2) For sure , we are returning a list, then whats the harm in having the return type as List than IEnumerable
Hi @TechQuery, So it's not so much "interchangeable" but the fact that List<T> implements IEnumerable<T> (msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx) but so does a lot of things. If you return an interface then anything which implements it can be used to consume it however if a more concrete implementation eg List<T> is used then it limits what can be consumed.
It is fun but your "cartypes" will never be a null. It is always a list with zero or more elements.
1

You should use "Where" Instead of "Select".

CarTypes cartypes = db.CarTypes.Where(t => t.TyreID == id).AsEnumerable();

"Select" is for specifying which data should be returned for each record, not for filtering the records. Your query with "Select" returns boolean values: false for records with TyreID != id and true for one record where TyreID = id :)

2 Comments

also, don't you want to return cartypes.AsEnumerable()
Hi - I do have .AsEnumerable() at the end of the Linq statement - but whether I add cartypes.AsEnumerable() or not, the error still remains on the linq statement. Thanks again, Mark.
1

Shouldn't you have:

IEnumerable<CarTypes> cartypes = db.CarTypes.Where(t => t.TyreID == id).AsEnumerable();

Instead of:

CarTypes cartypes = db.CarTypes.Select(t => t.TyreID == id).AsEnumerable();

Note: I would have made this a comment under PanJanek's answer but I'm not currenlty allowed beacuse of my low reputation...

1 Comment

Thanks a lot - that's what I was missing (IEnumerable at the start of the line) - PanJanek/Mike - thank you also for helping - cheers, Mark

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.