0

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.

4

2 回答 2

1

问题是内部查询返回IEnumerable<decimal>而不是单个值。

如果保证您的内部查询只返回一条记录,您可以简单地调用Single()

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

如果可以从内部查询返回多个值,那么您需要确切地弄清楚该比较应该如何工作并应用适当的聚合函数。

于 2013-05-11T17:28:43.297 回答
0

只需在内部查询末尾添加 Max:

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,
    };
于 2013-05-11T18:11:23.567 回答