John Schultz
John Schultz

Reputation: 672

How to do a nested select in where clause, in LINQ?

Consider the following SQL Statement:

SELECT  tfd.[ID],
        tfd.[TaxFormID],
        tfd.[TaxCodeLineNumber],
        tfd.[TaxCodeLineDescription]
FROM    [TaxCode].[TaxFormDetail] tfd
WHERE   tfd.[TaxFormID] = (SELECT TOP 1 cri.[TaxFormID] FROM [settings].[CompanyReportInformation] cri)

The database has one record in the "cri" table and I would like to do a top one record just as a mater of precaution.

I have the following LINQ statement that does not work and is where I need the help.

var _query = (
from tfd in TaxFormDetails
where tfd.TaxFormID ==
(from cri in CompanyReportInformation select new {cri.TaxFormID}).Take(1)
select new {tfd.ID, tfd.TaxFormID, tfd.TaxCodeLineNumber, tfd.TaxCodeLineDescription});
_query.Dump();

I get the following error:

Operator '==' cannot be applied to operands of type 'System.Guid?' and 'System.Linq.IQueryable'

What do I need to do to my LINQ statement to get this to work?

Upvotes: 1

Views: 1735

Answers (2)

Aducci
Aducci

Reputation: 26694

... select cri.TaxFormID).FirstOrDefault()

Upvotes: 1

Mark Brackett
Mark Brackett

Reputation: 85665

You're wrapping your inner select into an anonymous type:

 select new {cri.TaxFormID}

should be

 select cri.TaxFormID

Upvotes: 1

Related Questions