开发者

Selecting an object using Linq from an ordered result

Pretty simple question but I'm having a rough morning it seems.

I have the following query built out so far, and I'd like to select the first object in the returned result, but not the ID itself, but the underlying User th开发者_高级运维at has that ID.

Does that make sense?

//Edited the code for clarity!
                                                            After this OrderBy is run,
                                                            I have collection I want,
                                                        but I need to select the USER 
                                                             no the UserAuctionLance.
var user = db.UserAuctionLances.Where(a => a.AuctionId == id).OrderByDescending(s => s.DateTimeOfLance).Select??

"Order all of the user bids by descending date, and give me the last user who bid on auction number Foo."

Maybe that makes the question clearer. I need to return a User object.

Thank you for your time.


I believe you want:

var user = db.UserAuctionLances.Select(a=>a).Where(a => a.AuctionId == id).OrderByDescending(s => s.DateTimeOfLance).FirstOrDefault();

That should return the first result, or null if none exist.

And as you explained below, you want the user object within a userAuctionLances so something more like:

var user = db.UserAuctionLances.Where(a => a.AuctionId == id).OrderByDescending(s => s.DateTimeOfLance).Select(a=>a.UserObject).FirstOrDefault();


var user = db.UserAuctionLances.Where(a => a.AuctionId == id).OrderByDescending(s => s.DateTimeOfLance).Select(a => a.User).FirstOrDefault();

This will sort the matches and then return the first item that matches or null if it's a reference type (class) if not found.


You might wanna try ..

var user = db.UserAuctionLances.Where(a => a.AuctionId == id).OrderByDescending(s => s.DateTimeOfLance).Select(s => s.User).FirstOrDefault();

or

var user = db.UserAuctionLances.Where(a => a.AuctionId == id).OrderByDescending(s => s.DateTimeOfLance).Select(s => s.User).LastOrDefault();

Hard to tell, cause i hardly know what ur data structure is like.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜