In the Entity Framework you can run and bind custom queries on-the-fly like so…
protected class NitrogenMoisutreContainer
{
public double MinN { get; set; }
public double MaxN { get; set; }
public double MinM { get; set; }
public double MaxM { get; set; }
}
// ...
var q = dbcontext.Database.SqlQuery<NitrogenMoisutreContainer>(@"SELECT MAX(NitrogenBalance) as MaxN, MIN(NitrogenBalance) as MinN, MAX(FCWaterPercent) as MaxM, MIN(FCWaterPercent) as MinM
FROM agZoneProjectionGrowthStages
WHERE NitrogenBalance > 0 AND FCWaterPercent > 0").First();
The problem is that, to me, this feels messy. I had to create this class for one query and I will never use it again for anything else. The results are used exactly one line down from where it is executed.
Is there a way I can return an anonymous type? Even if I had to declare it first, like this…
var anonItem = new {
MinN = 0d,
MaxN = 0d,
MinM = 0d,
MaxM = 0d
};
var q = dbcontext.Database.SqlQuery<anonItem.GetType()>("...");
I just can’t figure out how to pass in my anonymous type’s Type as T. Is it possible?
I don’t see this as a problem.
You could create a tuple instead.