Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

up vote 2 down vote accepted

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.

share|improve this answer
    
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 –  Mark Tait Jun 8 '12 at 11:36
    
Fixed; see edit. Changed the variable declaration to var. –  WestDiscGolf Jun 8 '12 at 13:56
    
thanks a lot. Mark –  Mark Tait Jun 9 '12 at 17:41

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...

share|improve this answer
    
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 –  Mark Tait Jun 8 '12 at 10:43

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 :)

share|improve this answer
    
also, don't you want to return cartypes.AsEnumerable() –  Mike Simmons Jun 8 '12 at 10:18
    
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. –  Mark Tait Jun 8 '12 at 10:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.