I’m trying to convert the following SQL into Linq but getting confused when I try to apply the min. Basically I have a table which contains Beams and their allowed loads. I then query the database and Find the smallest beam by type, which has the required strength. The following 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;
returns the smallest beam by type where they have the required strength.
I already have the beams loaded into a Dictionary keyed by their IDs, so thought I should be able to query that object rather than make another call to the database.
My dictionary is
Dictionary<int, Beam>;
I’m trying something like this but getting confused how I get just the smallest beam by each Type.
var Beams = db.Values.Where(specificBeam => specificBeam.Ix >= iRequired)
.GroupBy(specificBeam => specificBeam.ElementType)
.Select(sb => new { sb.Key, MinIActual = sb.Min(specificBeam => specificBeam.Ix) });
Any pointers? Can I nest a First combined with
This has now been tested in a LINQPad example here.
You can then loop over like so: