Skip to content

Instantly share code, notes, and snippets.

@marshall007
Created March 8, 2013 21:11
Show Gist options
  • Select an option

  • Save marshall007/5119874 to your computer and use it in GitHub Desktop.

Select an option

Save marshall007/5119874 to your computer and use it in GitHub Desktop.
selecting min and max values while avoiding multiple enumerations.
var tests = dbTests.Select(t => new TestSearchable
{
Price = t.LabTests.Select(l => l.ListPrice)
.GroupBy(l => 0)
.Select(l => new Dictionary<string, int>
{
{ "High", l.Max() },
{ "Low", l.Min() }
}).First(),
TurnAround = t.LabTests.SelectMany(l => new[] { l.EstimatedTatLow, l.EstimatedTatHigh })
.GroupBy(l => 0)
.Select(l => new Dictionary<string, int>
{
{ "High", l.Max() },
{ "Low", l.Min() }
}).First(),
// ...
}.PopulateWith(t));
var tests = dbTests.Select(t => new TestSearchable
{
Price = new Dictionary<string, int>
{
{ "High", t.LabTests.Max(l => l.ListPrice) },
{ "Low", t.LabTests.Min(l => l.ListPrice) }
},
TurnAround = new Dictionary<string, int>
{
{ "High", t.LabTests.SelectMany(l => new[] { l.EstimatedTatLow, l.EstimatedTatHigh }).Max() },
{ "Low", t.LabTests.SelectMany(l => new[] { l.EstimatedTatLow, l.EstimatedTatHigh }).Min() }
},
// ...
}.PopulateWith(t));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment