Reputation: 622
These two queries are identical except for the table being queried. What is the best way for me to simplify this to eliminate the duplicated code?
//These two queries are identical except for the table name
var values = //Check for analog values
from a in historianDB.tblActualValueFloats
where a.PointSliceID == pointSliceID
where a.UTCDateTime >= startDate && a.UTCDateTime < endDate.AddDays(1)
orderby a.UTCDateTime
select new Record(a.UTCDateTime.ToLocalTime(), a.ActualValue);
if (values.Count() == 0)//If no analog records exist, check for digital values.
{
values =
from a in historianDB.tblActualValueDigitals
where a.PointSliceID == pointSliceID
where a.UTCDateTime >= startDate && a.UTCDateTime < endDate.AddDays(1)
orderby a.UTCDateTime
select new Record(a.UTCDateTime.ToLocalTime(), a.ActualValue);
}
Upvotes: 0
Views: 45
Reputation: 242
You won't get them much simpler than that other than perhaps using a union and then sorting it out in memory, but the code will be ugly.
You have a pretty clean way of doing it there, bearing in mind that the schema of the tables does differ slightly.
The only optimisation I would recommend is instead of doing a .Count()==0, try using .Any().
That will translate to an 'Exists' in SQL, which is more efficient than counting, as it returns as soon as it finds a record rather than traversing the table to count them.
Upvotes: 1