Danish Ahmad
Danish Ahmad

Reputation: 75

Linq Nested Query Issue with decimal type

I'm applying this following query in C#:

var query = from b in db.SalesOrderHeaders
        where b.SubTotal >  (from c in db.Employees
                             join v in db.EmployeePayHistories 
                         on c.BusinessEntityID equals v.BusinessEntityID
                             select v.Rate)
        select new
        {
            b.BusinessEntityID,
            b.SubTotal,
        };

But an error is returned: linq and face error: Operator '>' cannot be applied to operands of type 'decimal' and 'System.Linq.IQueryable<decimal>'.

Both b.subtotal and v.rate are decimal type and I want to compare these two. Any help is appreciated.

Upvotes: 0

Views: 147

Answers (2)

Justin Niessner
Justin Niessner

Reputation: 245509

The problem is that the inner query returns IEnumerable<decimal> rather than a single value.

If there is guaranteed to be only one record returned from your inner query, you could simply call Single():

where b.SubTotal > (from c in db.Employees
                    join v in db.EmployeePayHistories
                    on c.BusinessEntityID equals v.BusinessEntityID
                    select v.Rate).Max()

If more than one value can be returned from the inner query, then you'll need to figure out exactly how that comparison should work and apply the appropriate aggregate function.

Upvotes: 1

Shahar G.
Shahar G.

Reputation: 1510

Just add Max at the end of the inner query:

var query = from b in db.SalesOrderHeaders
    where b.SubTotal >  (from c in db.Employees
                         join v in db.EmployeePayHistories 
                     on c.BusinessEntityID equals v.BusinessEntityID
                         select v.Rate).Max()
    select new
    {
        b.BusinessEntityID,
        b.SubTotal,
    };

Upvotes: 0

Related Questions