具有十进制类型的Linq嵌套查询问题

时间:2013-05-11 17:18:25

标签: c# linq

我在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,
        };

但是会返回错误:linq and face error: Operator '>' cannot be applied to operands of type 'decimal' and 'System.Linq.IQueryable<decimal>'

b.subtotalv.rate都是十进制类型,我想比较这两个。任何帮助表示赞赏。

2 个答案:

答案 0 :(得分: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()

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

答案 1 :(得分: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,
    };