Question

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;
Was it helpful?

Solution

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top