Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 帮助将我的SQL查询转换为LINQ?_C#_Sql_Linq - Fatal编程技术网

C# 帮助将我的SQL查询转换为LINQ?

C# 帮助将我的SQL查询转换为LINQ?,c#,sql,linq,C#,Sql,Linq,我需要一些帮助来将此查询转换为LINQ: SELECT st.ProductId, st.ProductType, st.StockValue, st.InOut, st.SupplierCmdId, st.CreationDate, (SELECT su.Id FROM Suppliers AS su WHERE su.Id = (SELECT suo.SupplierId FROM SupplierOrders AS suo

我需要一些帮助来将此查询转换为LINQ:

SELECT
    st.ProductId, st.ProductType, st.StockValue, st.InOut, st.SupplierCmdId, st.CreationDate,
    (SELECT su.Id FROM Suppliers AS su 
     WHERE su.Id = (SELECT suo.SupplierId FROM SupplierOrders AS suo 
                    WHERE suo.Id = st.SupplierCmdId)) AS SupplerId
FROM 
    StockDetails AS st 
WHERE 
    st.ProductType = 'Yarn' 
    AND st.ProductId = 2835 
ORDER BY 
    st.CreationDate DESC
输出:

ProductId   ProductType StockValue  InOut   SupplierCmdId   CreationDate                SupplerId
2835        Yarn        10          1       1450            2020-03-12 15:25:54.000     151
2835        Yarn        5           0       NULL            2019-03-04 00:00:00.000     NULL
2835        Yarn        5           0       NULL            2018-12-23 00:00:00.000     NULL
2835        Yarn        10          1       1398            2018-12-17 10:51:17.000     151

提前谢谢

我试过:

var ProductType = "Yarn";
var ProductId = 2835;

stocks = (from st in _context.StockDetails
join sn in _context.StockStatus on st.StatusId equals sn.Id
where st.ProductId == ProductId 
      && st.ProductType == ProductType 
orderby st.CreationDate descending
select new StockList
    {
    StockValue = st.StockValue,
    InOut = st.InOut,
    SupplierCmdId = st.SupplierCmdId,
    CreationDate = st.CreationDate
    });

在这方面,我需要找到供应商Id(请参见SQL查询)

我认为这应该是等效的:

var productType = "Yarn";
var productId = 2835;
var query =
    from st in ctx.StockDetails
    where st.ProductType == productType
    where st.ProductId == productId
    orderby st.CreationDate descending
    let suppliers =
        from suo in ctx.SupplierOrders
        join su in ctx.Suppliers on suo.SupplierId equals su.Id
        where suo.Id == st.SupplierCmdId
        select su
    from su in suppliers.DefaultIfEmpty()
    select new
    {
        st.ProductId,
        st.ProductType,
        st.StockValue,
        st.InOut,
        st.SupplierCmdId,
        st.CreationDate,
        SupplierId = su.Id,
    };

这回答了你的问题吗<代码>var ProductId=“纱线”产品id看起来像示例dataSorry中的数字。粘贴不正确!我不知道为什么人们总是想把SQL转换成linq。SQL对于查询数据库来说比linq要好得多。Linq,尤其是当与EF一起使用时,往往会破坏您的查询,使其很难优化。@Liam-试图在这里找到我的方法。我对SQL比较熟悉,但它似乎使用了.net和EF,linq有一些优势,谢谢@Jeff Mercado。它给了我一个cs1941c#join子句中的一个表达式的类型不正确。类型推断在调用“Join”时失败。我必须在where子句之前向上移动let,它工作得非常好。再次感谢@Jeff Mercado