views:

29

answers:

1

Is this possible with Code First? I could do this if I only used Entity Framework:

var q = from m in context.Products
            .Top("0")
             select m;
A: 

Code-first is more about how you define your model and map it to your database. Querying is a separate topic entirely, and whether you use model-first, code-first or the new "POCO" support in the CTP, querying should be exactly the same.

In your case, assuming you have a querying problem rather than a code-first problem, I think you'd write it slightly differently as:

var query = context.Products.Take(1);

Although if you're doing this you probably want the actual item itself, so this might be more appropriate:

Product product = context.Products.Take(1).SingleOrDefault();

if (product == null) // Do something...

DoSomethingWithProduct(product);
Neil Barnwell
Thank you, using Take() does work. Maybe I'm missing something, because Top() does not work when I switched over to Code First, querying over IDbSet<type> (POCO) instead of an entity generated by the Entity Framework designer. Take() uses the extension IQueryable<type>, while Top() uses System.Data.Objects.ObjectQuery. Is it still possible to use Top() somehow?
Buginator