1

我正在尝试将以下 SQL 转换为 Linq,但是当我尝试应用 min 时会感到困惑。基本上我有一个包含梁及其允许负载的表格。然后我查询数据库并按类型查找具有所需强度的最小梁。以下 t-SQL

select
    sed.SEDetailID
from
    dbo.StructuralElementDetail sed
    inner join (select StructuralElementID, min(Ix) as MinIX from dbo.StructuralElementDetail where Ix >= @iRequired group by StructuralElementID) o
        on sed.StructuralElementID = o.StructuralElementID
            and sed.Ix = o.MinIX
order by
    StructuralElementID,
    Sequence;

按类型返回具有所需强度的最小梁。

我已经将梁加载到由其 ID 键入的字典中,因此认为我应该能够查询该对象,而不是再次调用数据库。

我的字典是

Dictionary<int, Beam>;

我正在尝试这样的事情,但对我如何获得每种类型的最小光束感到困惑。

            var Beams = db.Values.Where(specificBeam => specificBeam.Ix >= iRequired)
                .GroupBy(specificBeam => specificBeam.ElementType)
                    .Select(sb => new { sb.Key, MinIActual = sb.Min(specificBeam => specificBeam.Ix) });

任何指针?我可以嵌套一个 First 与

4

1 回答 1

2

现在已在此处的 LINQPad 示例中对此进行了测试。

var smallestBeamForTypes = 
    from anyBeam in db.Values
    where anyBeam.Ix >= iRequired
    group anyBeam by anyBeam.ElementType into beamTypeGroup
    let minIx = beamTypeGroup.Min(beam => beam.Ix)
    select new {
        ElementType = beamTypeGroup.Key,
        SmallestBeam = beamTypeGroup.First(beam => beam.Ix == minIx)
    };

然后你可以像这样循环:

foreach(var smallestBeamForType in smallestBeamForTypes)
{
    Console.WriteLine("For the element type {0} the smallest beam is {1}",
        smallestBeamForType.ElementType, smallestBeamForType.SmallestBeam);
}
于 2012-10-11T23:11:26.823 回答