RE: LINQ select nullable Id

2013-03-04 Thread James Chapman-Smith
Try this:

   int? id =
  (from t in things where t.Name == Foo select t.Id)
  .ToArray()
  .Concat(new int?[] { null })
  .First();

Cheers.

James.

From: ozdotnet-boun...@ozdotnet.com [mailto:ozdotnet-boun...@ozdotnet.com] On 
Behalf Of Greg Keogh
Sent: Monday, 4 March 2013 18:20
To: ozDotNet
Subject: LINQ select nullable Id

Folks, I want to select the int Id of an entity in a DbSet, or int? null if 
it's not found. Like this wrong sample:

int? id = (from t in things where t.Name == Foo select t.Id).FirstOrDefault();

In this case I get int zero if there is no match but I want null. Is there some 
way of rearranging this to get an Id or null? Remember that the query has to 
convertible down to SQL.

Greg K


Re: LINQ select nullable Id

2013-03-04 Thread David Rhys Jones
Hi Greg.
FirstOrDefault() will return 0 for non null Ints,  So I'm guessing that
t.Id is an (int).

So you're going to have to do something like this.

ListAB abs = new ListAB();
for (int i = 0; i  10; i++)
{
abs.Add(new AB { Id = i });
}

int? id = null;
IEnumerableint output = from t in abs where t.Id == 1000
select t.Id;
if(output.Any())
{
id = output.First();
}

Assert.IsNull(id);

or This.

public static int? FirstOrNull(this IEnumerableint query)
{
if (query.Any())
{
return query.First();
}

return null;
}

 int? alternative = (from t in abs where t.Id == 1000 select
t.Id).FirstOrNull();

hth.
Davy,

The US Congress voted Pizza sauce a vegetable. Don't even try to convince
me of anything in the states is sane any more!


On Mon, Mar 4, 2013 at 11:20 AM, James Chapman-Smith 
ja...@chapman-smith.com wrote:

  Try this:

 ** **

int? id =

   (from t in things where t.Name == Foo select t.Id)

   .ToArray()

   .Concat(new int?[] { null })

   .First();

 ** **

 Cheers.

 ** **

 James.

 ** **

 *From:* ozdotnet-boun...@ozdotnet.com [mailto:
 ozdotnet-boun...@ozdotnet.com] *On Behalf Of *Greg Keogh
 *Sent:* Monday, 4 March 2013 18:20
 *To:* ozDotNet
 *Subject:* LINQ select nullable Id

 ** **

 Folks, I want to select the int Id of an entity in a DbSet, or int? null
 if it's not found. Like this *wrong* sample:

  

 int? id = (from t in things where t.Name == Foo select
 t.Id).FirstOrDefault();

  

 In this case I get int zero if there is no match but I want null. Is there
 some way of rearranging this to get an Id or null? Remember that the query
 has to convertible down to SQL.

  

 Greg K



Re: LINQ select nullable Id

2013-03-04 Thread Mark Hurd
int? id = (from t in things where t.Name == Foo select new
int?(t.Id)).FirstOrDefault();


On 4 March 2013 18:19, Greg Keogh g...@mira.net wrote:
 Folks, I want to select the int Id of an entity in a DbSet, or int? null if
 it's not found. Like this wrong sample:

 int? id = (from t in things where t.Name == Foo select
 t.Id).FirstOrDefault();

 In this case I get int zero if there is no match but I want null. Is there
 some way of rearranging this to get an Id or null? Remember that the query
 has to convertible down to SQL.

 Greg K



--
Regards,
Mark Hurd, B.Sc.(Ma.)(Hons.)