Created
June 27, 2013 16:13
-
-
Save martijnburgers/5877818 to your computer and use it in GitHub Desktop.
custom map example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Linq.Expressions; | |
using System.Web.Mvc; | |
using Delta.Common.Reflection; | |
namespace CustomMap | |
{ | |
public class ServiceDataStatistic | |
{ | |
public DateTime? Date { get; set; } | |
public long? In { get; set; } | |
public long? Out { get; set; } | |
} | |
public class GeneralStatistic | |
{ | |
public DateTime? Date { get; set; } | |
public long? Bytes { get; set; } | |
} | |
public static class DataGen | |
{ | |
public static IEnumerable<ServiceDataStatistic> GetStatisticsFromServer() | |
{ | |
yield return new ServiceDataStatistic | |
{ | |
Date = DateTime.Now.AddMonths(-1), | |
In = 100, | |
Out = 200, | |
}; | |
yield return new ServiceDataStatistic | |
{ | |
Date = DateTime.Now.AddMonths(-2), | |
In = 50, | |
Out = 100, | |
}; | |
} | |
} | |
public static class Extensions | |
{ | |
public static IEnumerable<GeneralStatistic> GetGeneralStatistic( | |
this IEnumerable<ServiceDataStatistic> statistics, Expression<Func<ServiceDataStatistic, long?>> expression) | |
{ | |
var nameForExpression = ExpressionHelper.GetExpressionText(expression); | |
return statistics.Select(s => new GeneralStatistic | |
{ | |
Date = s.Date, | |
Bytes = s.GetPropValue<long>(nameForExpression) | |
}); | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var a = DataGen.GetStatisticsFromServer().GetGeneralStatistic(s1 => s1.In); | |
var b = DataGen.GetStatisticsFromServer().GetGeneralStatistic(s2 => s2.Out); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@ Mathias: Deze code gaat er vanuit dat de expression dus een property bevat, anders gaat het verder fout bij s.GetPropValue. Dit kan je evt zelf safer maken. Een goede achtergrond is mijn artikel: http://www.martijnburgers.net/post/2011/06/05/The-basics-behind-static-reflection.aspx