Assuming ID is the primary key, I think using Find , and living with the null check, might be more efficient because you are then leaning on the first level cache.
On Thu, Jul 26, 2012 at 10:05 PM, Michael Minutillo < [email protected]> wrote: > Try > > int? foundId = context.Things.Where(t => t.ID == lookupID).Select(t => > t.ID).SingleOrDefault(); > > > > Michael M. Minutillo > Indiscriminate Information Sponge > http://codermike.com > > > > On Thu, Jul 26, 2012 at 7:26 PM, Greg Keogh <[email protected]> wrote: > >> I have to find an object in an EF4 entity collection with a specific ID >> property and return the int value if it’s found and return (int?)null if >> otherwise. This skeleton code crashes of course if the ID isn’t found.*** >> * >> >> ** ** >> >> int? foundId = context.Things.SingleOrDefault(t => t.ID == lookupID).ID;* >> *** >> >> ** ** >> >> So what is the best way of recoding this elegantly as a one-liner so it >> gives me the ID or null when not found? It has to be a one-liner because >> it’s in a select clause.**** >> >> ** ** >> >> Greg**** >> > > -- Heinrich Breedt “Do not wait to strike till the iron is hot; but make it hot by striking.” - William B. Sprague
