var t = ( from t in things where t.id = lookupID select t.id
).FirstOrDefault();

Because I prefer the SQL syntax.

Davy

Sent via telegraph.

On 26 Jul 2012, at 14:47, Wallace Turner <[email protected]> wrote:

 that doesnt work Mike?
did you mean:
int? foundId = context.Things.Where(t => t.ID = lookupID).Select(t => *
(int?)* t.ID).SingleOrDefault();


On 26/07/2012 8:05 PM, Michael Minutillo 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
>

Reply via email to