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****
>

Reply via email to