将SQL转换为Linq select在哪里

时间:2017-09-29 06:50:39

标签: sql linq sql-to-linq-conversion

我有三张桌子:

TABLE_PRODUCT (IdProduct, ProductName, ProductUnit) 
TABLE_STORE_HOUSE (IdContain, IdProduct, ProductNumber, TimeInput) 
TABLE_SELL (IdSell, IdContain, ProductNumberSell, TimeSell)

当前,如何使用LinQ查询获取TABLE_STORE_HOUSE.IdProduct女巫条件TABLE_STORE_HOUSE.ProductNumber - Sum(TABLE_SELL.ProductNumberSell) > 0TABLE_STORE_HOUSE.TimeInput最小

帮我转换Sql到Linq ..............

select top 1 IdContain 
from 
    TABLE_STORE_HOUSE 
where IdProduct = '6' 
      and 
        ProductNumber - (select sum(ProductNumber) 
                         from TABLE_SELL 
                         Where TABLE_SELL.IdContain = IdContain)> 0 
order by TimeInput desc;

1 个答案:

答案 0 :(得分:1)

你能试试吗?

from t in TABLE_STORE_HOUSEs 
let TSell =  (
                from s in TABLE_SELLs 
                where s.IdContain == t.IdContain
                orderby s.ProductNumber
                select new { 
                         s.ProductNumber
                        }
               )
where t.IdProduct == 6 && (t.ProductNumber - TSell.Sum(si => si.ProductNumber)) > 0
select new { t.IdContain }

对于前1名,您可以使用Take()功能。