Currently im trying to make my query short with reusable peice of code like this to check for post if it’s eligible to display.
// Logic to check if post is eligible for display
public bool isEligibleForDisplay(Post n)
{
var pubDate = n.PUBLISH_DATE ?? DateTime.MinValue;
var endDate = n.END_DATE ?? DateTime.MaxValue;
var correctState = (n.STATE == PostState.Publish || n.STATE == PostState.Furture);
var dateInRange = (DateTime.Now >= pubDate && DateTime.Now <= endDate);
return correctState && dateInRange;
}
my linq look like this:
var q = from n in _db.Posts
where isEligibleForDisplay(n)
group n by n.POST_ID into g
select g.OrderByDescending(t => t.CREATE_DATE).First();
return q.Take(quantity);
I ran into “No supported translation in SQL” problem for the first time of using linq to sql, I am just wondering if there are anyway that can use as a work around for this case, which could be troublesome if I include whole lot of those checking logic into my query everytime.
I’m so looking forward for a reply. Thanks!
You can create a function on your SQL server called
isEligibleForDisplaythat does the SQL equivalence of these checkes and add that to your dbml file.I haven’t tested this, but I’m thinking the easiest would be if you create a function where you pass the values you want, rather than the whole record, and I think something like this might work: