var method = new Func<double, double, double>(Math.Pow).Method; var parameter = Expression.Parameter(typeof(object[])); var args = method.GetParameters() .Select((param, i) => Expression.Convert( Expression.ArrayIndex(parameter, Expression.Constant(i)), param.ParameterType)); var expr = Expression.Lambda<Func<object[], object>>( Expression.Convert(Expression.Call(method, args), typeof(object)), parameter); var invoker = expr.Compile(); var result = invoker(new object[] {Math.PI, 2.0});
Thursday, February 16, 2012
Create a fast compiled method invoker using LINQ
Friday, February 10, 2012
Create a Google calendar event in an existing calendar
var credentials = new GDataCredentials(userName, password); var service = new CalendarService(null) {Credentials = credentials}; var query = new CalendarQuery("https://www.google.com/calendar/feeds/default/owncalendars/full"); var feed = service.Query(query); var eventFeedLink = feed.Entries.Cast<CalendarEntry>() .Where(item => item.Title.Text == calendarName) .SelectMany(item => item.Links) .First(link => link.Rel == GDataParserNameTable.NSGCal + "#eventFeed"); var entry = new EventEntry(title) {Times = {new When(start, end, allDay)}}; service.Insert(new Uri(eventFeedLink.AbsoluteUri), entry);Google Data API .NET client library
Thursday, February 2, 2012
Page through potentially large NHibernate criteria query results
public static IEnumerable<T> Enumerate<T>(this ICriteria criteria, int pageSize = 1000) { var entityCount = CriteriaTransformer.TransformToRowCount(criteria).UniqueResult<int>(); var pageCount = (int) Math.Ceiling(entityCount/(double) pageSize); criteria = CriteriaTransformer.Clone(criteria).SetMaxResults(pageSize); return Enumerable.Range(0, pageCount) .SelectMany(pageNum => criteria.SetFirstResult(pageNum*pageSize).List<T>()); }NHibernate
Subscribe to:
Posts (Atom)