转盘,LINQ语法

本文关键字:语法 LINQ 转盘 | 更新日期: 2023-09-27 18:18:09

我有以下SQL和结果:

SELECT*FROM StockLines WHERE ID in('A','B') AND Stock>=1100 AND Stock<=1105
-- RESULT
Stock   ID  Value
1100    A   398.02
1100    B   1803.62
1101    A   480.07
1101    B   1794.89
1102    A   583.24
1102    B   1800.43
1103    A   528.9
1103    B   1808.26
1104    A   556.14
1104    B   1801.77
1105    A   454.87
1105    B   1778.83

我使用以下SQL来旋转表:

SELECT
    Stock, 
    MIN (CASE ID WHEN 'A' THEN VALUE END) [A],
    MIN (CASE ID WHEN 'B' THEN VALUE END) [B]
FROM 
    StockLines 
WHERE 
    Stock>=1100 AND Stock<=1105
GROUP BY Stock
-- RESULT
Stock   A   B
1100    398.02  1803.62
1101    480.07  1794.89
1102    583.24  1800.43
1103    528.9   1808.26
1104    556.14  1801.77
1105    454.87  1778.83

问题是:如何在LINQ中编写第二个SQL ?

转盘,LINQ语法

我使用了LINQ to Objects,但是这个语法应该可以很好地与LINQ to SQL:

void Main()
{
    var items = new List<StockItem>();
    items.Add(new UserQuery.StockItem { ID = "A", Value = 398.02m, Stock = 1100});
    items.Add(new UserQuery.StockItem { ID = "B", Value = 1803.62m, Stock = 1100});
    items.Add(new UserQuery.StockItem { ID = "A", Value = 480.07m, Stock = 1100});
    items.Add(new UserQuery.StockItem { ID = "B", Value = 1794.89m, Stock = 1100});
    items.Add(new UserQuery.StockItem { ID = "A", Value = 583.24m, Stock = 1100});
    items.Add(new UserQuery.StockItem { ID = "B", Value = 1800.43m, Stock = 1100});
    var query = from item in items
                group item by new { item.Stock }
                into stocks
                select new { 
                    Stock = stocks.Key.Stock, 
                    A = stocks.Where(stock => stock.ID == "A").Min(stock => stock.Value), 
                    B = stocks.Where(stock => stock.ID == "B").Min(stock => stock.Value) 
                };
}
public class StockItem
{
    public int Stock { get; set; }
    public decimal Value { get; set; }
    public string ID { get; set; }
}

如果我们意识到这个SQL片段

MIN (CASE ID WHEN 'A' THEN VALUE END)

只是

的快捷方式
MIN (CASE ID WHEN 'A' THEN VALUE ELSE NULL END)

那么相应的Linq查询自然会像这样

    var StockLines = new[]
    {
        new { Stock = 1100, ID = "A", Value = 398.02m },
        new { Stock = 1100, ID = "B", Value = 1803.62m },
        new { Stock = 1101, ID = "A", Value = 480.07m },
        new { Stock = 1101, ID = "B", Value = 1794.89m },
        new { Stock = 1102, ID = "A", Value = 583.24m },
        new { Stock = 1102, ID = "B", Value = 1800.43m },
        new { Stock = 1103, ID = "A", Value = 528.9m },
        new { Stock = 1103, ID = "B", Value = 1808.26m },
        new { Stock = 1104, ID = "A", Value = 556.14m },
        new { Stock = 1104, ID = "B", Value = 1801.77m },
        new { Stock = 1105, ID = "A", Value = 454.87m },
        new { Stock = 1105, ID = "B", Value = 1778.83m },
    };
    var query =
        from line in StockLines
        where line.Stock >= 1100 && line.Stock <= 1105
        group line by line.Stock into lines
        select new
        {
            Stock = lines.Key,
            A = lines.Min(item => item.ID == "A" ? (decimal?)item.Value : null),
            B = lines.Min(item => item.ID == "B" ? (decimal?)item.Value : null),
        };