code_junkie

Querying Complex Data Structure in Memory

Nov 12th, 2011
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. udr.bytesin > 1000 && (udr.analysis.discrepency > 100000 || udr.analysis.discrepency_percent > 100) && udr.started_date > '2008-11-10 22:00:44' order by udr.analysis.discrepency DESC LIMIT 10
  2.  
  3. class Udr { // formatted for space
  4. public int BytesIn { get; set; }
  5. public UdrAnalysis Analysis { get; set; }
  6. public DateTime StartedDate { get; set; }
  7. }
  8. class UdrAnalysis {
  9. public int Discrepency { get; set; }
  10. public int DiscrepencyPercent { get; set; }
  11. }
  12. static class Program {
  13. static void Main() {
  14. Udr[] data = new [] {
  15. new Udr { BytesIn = 50000, StartedDate = DateTime.Today,
  16. Analysis = new UdrAnalysis { Discrepency = 50000, DiscrepencyPercent = 130}},
  17. new Udr { BytesIn = 500, StartedDate = DateTime.Today,
  18. Analysis = new UdrAnalysis { Discrepency = 50000, DiscrepencyPercent = 130}}
  19. };
  20. DateTime when = DateTime.Parse("2008-11-10 22:00:44");
  21. var query = data.AsQueryable().Where(
  22. @"bytesin > 1000 && (analysis.discrepency > 100000
  23. || analysis.discrepencypercent > 100)
  24. && starteddate > @0",when)
  25. .OrderBy("analysis.discrepency DESC")
  26. .Take(10);
  27. foreach(var item in query) {
  28. Console.WriteLine(item.BytesIn);
  29. }
  30. }
  31. }
  32.  
  33. static IEnumerable<Foo> ReadFoos(string path) {
  34. return from line in ReadLines(path)
  35. let parts = line.Split('|')
  36. select new Foo { Name = parts[0],
  37. Size = int.Parse(parts[1]) };
  38. }
  39. static IEnumerable<string> ReadLines(string path) {
  40. using (var reader = File.OpenText(path)) {
  41. string line;
  42. while ((line = reader.ReadLine()) != null) {
  43. yield return line;
  44. }
  45. }
  46. }
Add Comment
Please, Sign In to add comment