Guest User

Untitled

a guest
Apr 21st, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. class SimpleObject
  2. {
  3. public int Id { get; set; }
  4. }
  5.  
  6. public IEnumerable<SimpleObject> GetMatches(int[] matchingIds)
  7. {
  8. // OH NOOOOOOES! This activates all 30,000 SimpleObjects. TOO SLOW!
  9. var query = from SimpleObject simple in db
  10. join id in matchingIds on simple.Id equals id
  11. select simple;
  12. return query.ToArray();
  13. }
  14.  
  15. IList<SimpleObject> objs = new List<SimpleObject>();
  16. foreach(var tbf in ids)
  17. {
  18. var result = from SimpleObject o in db()
  19. where o.Id = tbf
  20. select o;
  21.  
  22. if (result.Count == 1)
  23. {
  24. objs.Add(result[0]);
  25. }
  26. }
  27.  
  28. var simpleObjects = db.Query<SimpleObject>(typeof(SimpleObject));
  29.  
  30. private SimpleObject[] GetSimpleObjectUsingSodaAgainstAProperty(int[] matchingIds, IObjectContainer db)
  31. {
  32. SimpleObject[] returnValue = new SimpleObject[matchingIds.Length];
  33.  
  34. for (int counter = 0; counter < matchingIds.Length; counter++)
  35. {
  36. var query = db.Query();
  37. query.Constrain(typeof(SimpleObject));
  38. query.Descend("Id").Constrain(matchingIds[counter]);
  39. IObjectSet queryResult = query.Execute();
  40. if (queryResult.Count == 1)
  41. returnValue[counter] = (SimpleObject)queryResult[0];
  42. }
  43.  
  44. return returnValue;
  45. }
  46.  
  47. public class SimpleObject
  48. {
  49. private int _id;
  50.  
  51. public int Id {
  52. get
  53. { return _id; }
  54. set
  55. { _id = value; }
  56. }
  57. }
  58.  
  59. private SimpleObject[] GetSimpleObjectUsingSodaAgainstAField(int[] matchingIds, IObjectContainer db)
  60. {
  61. SimpleObject[] returnValue = new SimpleObject[matchingIds.Length];
  62.  
  63. for (int counter = 0; counter < matchingIds.Length; counter++)
  64. {
  65. var query = db.Query();
  66. query.Constrain(typeof(SimpleObject));
  67. query.Descend("_id").Constrain(matchingIds[counter]);
  68. IObjectSet queryResult = query.Execute();
  69. if (queryResult.Count == 1)
  70. returnValue[counter] = (SimpleObject)queryResult[0];
  71. }
  72.  
  73. return returnValue;
  74. }
  75.  
  76. GetAll: 2450 ms
  77. GetWithOriginalCode: 2694 ms
  78. GetWithSODAandProperty: 75373 ms
  79. GetWithSODAandField: 77 ms
  80.  
  81. from SimpleObject simple in db
  82. where matchingIds.Contains(simple.Id)
  83. select simple
  84.  
  85. var result1 = db.ObjectByID((SimpleObject t) => t.Id, 42, 77);
  86. var result2 = db.ObjectByID((SimpleObject t) => t.Id, myIDList);
  87. var result3 = db.ObjectByID((OtherObject t) => t.Name, "gamlerhart","db4o");
  88.  
  89. var result = from SimpleObject t
  90. where t.Id = 42 || t.Id==77 ... t.Id == N
  91. select t
  92.  
  93. public static class ContainerExtensions{
  94.  
  95. public static IDb4oLinqQuery<TObjectType> ObjectByID<TObjectType, TIdType>(this IObjectContainer db,
  96. Expression<Func<TObjectType, TIdType>> idPath,
  97. params TIdType[] ids)
  98. {
  99. if(0==ids.Length)
  100. {
  101. return db.Cast<TObjectType>().Where(o=>false);
  102. }
  103. var orCondition = BuildOrChain(ids, idPath);
  104. var whereClause = Expression.Lambda(orCondition, idPath.Parameters.ToArray());
  105. return db.Cast<TObjectType>().Where((Expression<Func<TObjectType, bool>>) whereClause);
  106. }
  107.  
  108. private static BinaryExpression BuildOrChain<TIdType, TObjectType>(TIdType[] ids, Expression<Func<TObjectType, TIdType>> idPath)
  109. {
  110. var body = idPath.Body;
  111. var currentExpression = Expression.Equal(body, Expression.Constant(ids.First()));
  112. foreach (var id in ids.Skip(1))
  113. {
  114. currentExpression = Expression.OrElse(currentExpression, Expression.Equal(body, Expression.Constant(id)));
  115. }
  116. return currentExpression;
  117. }
  118.  
  119. }
Add Comment
Please, Sign In to add comment