c# - DbLinq generic extension method - string as keyselector? -


the following snippet indicates want:

public static class dblinqextension {     public static int maxid<t>(this dblinq.data.linq.table<t> table) t : class     {         var val = table.orderbydescending(x => "id").firstordefault();         return convert.toint32(val);     } } 

with dbmetal i've generated mapping classes. every table have has column id (which integer) , want know max id.

anyone idea how can snippet working??

thanks!


i've found article: orderby string keyselector

with suggestion applied code become:

public static int maxid<t>(this dblinq.data.linq.table<t> table) t : class {     var val = table.orderbydescending(createselectorexpression<t>("id")).firstordefault();     return convert.toint32(val); }  private static expression<func<t, int32>> createselectorexpression<t>(string propertyname) t : class {     var parameterexpression = expression.parameter(typeof(t));     return (expression<func<t, int32>>)expression.lambda(         expression.propertyorfield(parameterexpression, propertyname),          parameterexpression     ); } 

but error:

value cannot null. parameter name: key

not sure try

var result = x in table select x.max ( x => x.id ); 

edit - if need take id string expression:

var result = x in table select x.max ( x => x.gettype().getproperty ("id").getgetmethod().invoke (x, null) ); 

edit - if need split up:

var result1 = x in table select x; var result2 = result1.max(x => x.gettype().getproperty("id").getgetmethod().invoke(x, null));  

edit - if "id" field need this:

var result1 = x in table select x;  var result2 = result1.max(x => x.gettype().getfield ( "id" ).getvalue(x)); 

Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -