开发者

How to use .Top() with Code First

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

var q = from m in context.P开发者_如何学Croducts
            .Top("0")
             select m;


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜