Trying to get a Guid? via linq

.net, c#, linq

Solution

Your query returns all matching guids and it has type `IQueryable<Guid?>`

IQueryable<Guid?> guids = 
    from R in Table1
    join P in Table2
    on R.Id equals P.Id2
    where R.Name == 'blah blah' 
    select R.Id;

If you need one guid, use `First`, `Single`, `FirstOrDefault`, or `SingleOrDefault`

Guild? guid = guids.FirstOrDefault();

Or in single statement:

Guid? guid = Table1.Where(R => R.Name == "blah")
                   .Join(Table2, R => R.Id, P => P.Id2, (R,P) => R.Id)
                   .FirstOrDefault();

Mixed syntax (unfortunately there is no equivalent for `FirstOrDefault` operator in query syntax):

Guid? guid =  (from R in Table1
               join P in Table2
               on R.Id equals P.Id2
               where R.Name == 'blah blah' 
               select R.Id).FirstOrDefault();

Problem

I need to get the id value from Table1 which is a Guid. This query can return a null also. So I started with following ``` Guid? SomeID = from R in Table1 join P in Table2 on R.Id equals P.Id2 where R.Name == 'blah blah' select R.Id; ``` But I get following compilation error. Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Guid?' Changing Guid? to Guid didn't help. ``` Guid SomeID = from R in Table1 join P in Table2 on R.Id equals P.Id2 where R.Name == 'blah blah' select R.Id; ``` as now I get following error. Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Guid' What am I doing wrong?

Original source