This is what I would like to work:
var myCollection = collection.Where(r =>
r.ChannelId == channelId &&
r.Type == ResourceType.RedundancyLock &&
r.GroupId == groupId &&
(r.OwnerId == "" ||
r.OwnerId == ownerId ||
r.LastUpdateTime < DateTime.UtcNow.Subtract(TimeSpan.FromSeconds(r.TimeDelta))));
Sadly, this results in an exception:
"Unsupported where clause: (r.LastUpdateTime < (Nullable<DateTime>)DateTime:(2014-08-18T20:26:44.6861998Z).Subtract(TimeSpan.FromSeconds((Double)r.TimeDelta)))."
The following query works:
var myCollection = collection.Where(r =>
r.ChannelId == channelId &&
r.Type == ResourceType.RedundancyLock &&
r.GroupId == groupId &&
(r.OwnerId == "" ||
r.OwnerId == ownerId ||
r.LastUpdateTime < DateTime.UtcNow.Subtract(TimeSpan.FromSeconds(10))));
Is there any way that I can derive a timespan from a value of a record?
The answer is to do a 2 stage query. Filter your there clause for as much as you can.
var hold = collection.Where(r =>
r.ChannelId == channelId &&
r.Type == ResourceType.RedundancyLock &&
r.GroupId == groupId);
Then pull down the data from the server
var holdList = hold.ToList();
Then Use your last Query
return holdList.Where(r => (r.OwnerId == "" ||
r.OwnerId == ownerId ||
r.LastUpdateTime < DateTime.UtcNow.Subtract(TimeSpan.FromSeconds(r.TimeDelta))));
It is less than optimal because you are pulling more from the database than you need. And also, You're using more memory on the web server than you would otherwise. But the end result should work.