tags:

views:

159

answers:

1

I am using Subsonic (SimpleRepository) to query my SQL 2008 database. I am trying to write a query that will calculate the number of days between two fields and return records where the difference is less than a given number. However, I get a "The member 'Days' is not supported" error.

Can anybody suggest an alternative query?

Here's the query I'm trying to run:

var repository = new SimpleRepository("MyConnection", 
                              SimpleRepositoryOptions.None);
var query = (from c in repository.All<Data.Customer>()
            where c.LastSynchronizedOn == null || 
                  (c.LastSynchronizedOn - c.CreatedOn).Days <= 7)
            select c).Distinct();

EDIT:

I tried:

(c.LastSynchronizedOn == null || (c.LastSynchronizedOn.Value - c.CreatedOn).Days <= 7)

I get the same exception: The member 'Days' is not supported

I also tried:

(c.LastSynchronizedOn == null || ((c.LastSynchronizedOn - c.CreatedOn) > new TimeSpan(7, 0, 0, 0)))

I get: Failed to convert parameter value from a TimeSpan to a String.

A: 

Please check the data type of fields LastSynchronizedOn and CreatedOn as the problem is not related to SubSonic but .Net is complaining (if this is right way to put it) because the expression (c.LastSynchronizedOn - c.CreatedOn) might not be returning DateTime object.

EDIT:- as per your comment please try this

(c.LastSynchronizedOn.Value - c.CreatedOn).Days

EDIT (2): the statement

(c.LastSynchronizedOn == null || 
  ((c.LastSynchronizedOn - c.CreatedOn) > new TimeSpan(7, 0, 0, 0)))

returns boolean (true or false)

try this:

var repository = new SimpleRepository("MyConnection", 
                          SimpleRepositoryOptions.None);
var query = (from c in repository.All<Data.Customer>()
 where ((c.LastSynchronizedOn ?? 
              new DateTime(c.CreatedOn.Year, c.CreatedOn.Month, 
                          c.CreatedOn.Day).AddDays(-7)) 
     - c.CreatedOn).Days <= 7) select c).Distinct(); 
TheVillageIdiot
LastSynchronizedOn is DateTime? and CreatedOn is DateTime
The reason why I believe Subsonic is throwing the error an not .NET is because the "Source" of the exception is "Subsonic.Core"
...and also the entry at the top of the stack trace is " at SubSonic.Linq.Structure.TSqlFormatter.VisitMemberAccess(MemberExpression m)"
I've edited answer
TheVillageIdiot
You suggestion yields the same error. I actually looked at the source for the VisitMemberAccess method in TSqlFormatter.cs in Subsonic 3.0.3 and I see on line 216 where NotSupportedException is thrown. That's why I'm looking for an alternate way to write my Linq statement.