I’ve added a custom property to a LINQ-to-SQL entity:
public partial class User
{
public bool IsActive
{
get
{
// note that startDate and endDate are columns of User table
return startDate <= DateTime.Now && endDate >= DateTime.Now;
}
}
}
and now I need to use this property with lambda expressions:
activeUsers = users.Where(u => u.IsActive);
but when this code is executed I get a System.NotSupportedException. The exception message says that “SQL conversions for the member ‘User.IsActive’ are not supported”.
Is there a way to solve this problem?
The
IsActiveyou show is regular C# – it will be compiled to IL and will not be available for LINQ-to-SQL (etc) to inspect and turn into TSQL to execute at the database. One option here might be:then you should be able to use:
Or similarly – maybe an extension method:
then:
The difference here is that we are using the
IQueryable<T>interface and expression trees throughout, which allows LINQ to understand our intent, and create suitable TSQL to achieve the same.