开发者

How do I convert a DBQuery<> (or ObjectQuery<>) to a non-collection primitive type (ie: int, string)

开发者 https://www.devze.com 2023-04-12 03:50 出处:网络
var lastitem = Contacts .OrderByDescending(c => c.ContactID) .Take(1).Select(p=>p.ContactID); lastitem is even though a single item returns a DBQuery<Int32>
var lastitem = Contacts
.OrderByDescending(c => c.ContactID)
.Take(1).Select(p=>p.ContactID);

lastitem is even though a single item returns a DBQuery<Int32>

is there a way to convert it to just a pure Int32?

Thanks for the help!

A开发者_如何学编程LSO:

is there a better way to do this? Basically, I'm trying to get an Int32 type from ContactID from the very last item inside the database


You want .First() (throws if empty) or .FirstOrDefault() (returns 0 if empty, or null if you add .Cast<int?() first):

int lastitem = Contacts.OrderByDescending(c => c.ContactID)
                       .Select(c => c.ContactID)
                       .First();
0

精彩评论

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

关注公众号