对iquerable的SQL查询

本文关键字:查询 SQL iquerable | 更新日期: 2023-09-27 18:13:29

我想问一下,我如何才能将这个SQL查询转换为IQueryable:

SQL查询:

  SELECT 
    [s].[Id] AS [Id], 
    [s].[PartId] AS [PartId], 
    [s].[Quantity] AS [Quantity], 
    [s].[OfficeId] AS [OfficeId], 
    [s].[StockLocationId] AS [StockLocationId], 
    [s].[StockSubLocationId] AS [StockSubLocationId],
    [p].[Description] AS [PartDescription],
    [p].[StdSellPrice] AS [PartSellPrice]
    FROM [dbo].[Stock] AS [s]
    INNER JOIN [dbo].[Part] AS p ON [p].Id = [s].PartId
    WHERE ([s].[StockLocationId] >= 1) AND ([s].[StockSubLocationId] <= 2)
到目前为止,我已经得到了这个IQueryable:
var getStockDetails = tempCtx.Stocks.Where(s => s.StockLocationId >= loc && s.StockSubLocationId <= subLoc);

对iquerable的SQL查询

var getStockDetails = 
    from s in tempCtx.Stocks
    join p in tempCtx.Part on s.PartId equals p.Id
    where s.StockLocationId >= 1 and s.StockSubLocationId <= 2
    select new { Id = s.Id, PartId = s.PartId, Quantity = s.Quantity };

只需为您想要返回的其他列添加select部分。

试试这个:

var q = tempCtx.Stocks.
            Join(tempCtx.Parts, x => x.PartID, y => y.ID, (x,y) => new{x=x,y=y}).
            Where(xy=>xy.x.StockLocationID >= 1 && xy.x.StockSubLocationID <= 2).
            Select(xy => new {
                              xy.x.ID, 
                              xy.x.PartID, 
                              xy.x.Quantity, 
                              xy.x.OfficeID, 
                              xy.x.StockLocationID, 
                              xy.x.StockSubLocationID, 
                              xy.y.Description, 
                              xy.y.StdSellPrice});