c# - Custom properties and lambda expressions -
i've added custom property linq-to-sql entity:
public partial class user { public bool isactive { { // note startdate , enddate columns of user table homecoming startdate <= datetime.now && enddate >= datetime.now; } } }
and need utilize property lambda expressions:
activeusers = users.where(u => u.isactive);
but when code executed system.notsupportedexception
. exception message says "sql conversions fellow member 'user.isactive' not supported".
is there way solve problem?
the isactive
show regular c# - compiled il , not available linq-to-sql (etc) inspect , turn tsql execute @ database. 1 alternative here might be:
public static expression<func<user,bool>> getisactivefilter() { homecoming user => user.startdate <= datetime.now && user.enddate >= datetime.now; }
then should able use:
activeusers = users.where(user.getisactivefilter());
or - maybe extension method:
public static iqueryable<user> activeonly(this iqueryable<user> users) { homecoming users.where(user => user.startdate <= datetime.now && user.enddate >= datetime.now); }
then:
activeusers = users.activeonly();
the difference here using iqueryable<t>
interface , expression trees throughout, allows linq understand our intent, , create suitable tsql accomplish same.
c# .net linq linq-to-sql lambda
No comments:
Post a Comment