Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class SimpleObject
- {
- public int Id { get; set; }
- }
- public IEnumerable<SimpleObject> GetMatches(int[] matchingIds)
- {
- // OH NOOOOOOES! This activates all 30,000 SimpleObjects. TOO SLOW!
- var query = from SimpleObject simple in db
- join id in matchingIds on simple.Id equals id
- select simple;
- return query.ToArray();
- }
- IList<SimpleObject> objs = new List<SimpleObject>();
- foreach(var tbf in ids)
- {
- var result = from SimpleObject o in db()
- where o.Id = tbf
- select o;
- if (result.Count == 1)
- {
- objs.Add(result[0]);
- }
- }
- var simpleObjects = db.Query<SimpleObject>(typeof(SimpleObject));
- private SimpleObject[] GetSimpleObjectUsingSodaAgainstAProperty(int[] matchingIds, IObjectContainer db)
- {
- SimpleObject[] returnValue = new SimpleObject[matchingIds.Length];
- for (int counter = 0; counter < matchingIds.Length; counter++)
- {
- var query = db.Query();
- query.Constrain(typeof(SimpleObject));
- query.Descend("Id").Constrain(matchingIds[counter]);
- IObjectSet queryResult = query.Execute();
- if (queryResult.Count == 1)
- returnValue[counter] = (SimpleObject)queryResult[0];
- }
- return returnValue;
- }
- public class SimpleObject
- {
- private int _id;
- public int Id {
- get
- { return _id; }
- set
- { _id = value; }
- }
- }
- private SimpleObject[] GetSimpleObjectUsingSodaAgainstAField(int[] matchingIds, IObjectContainer db)
- {
- SimpleObject[] returnValue = new SimpleObject[matchingIds.Length];
- for (int counter = 0; counter < matchingIds.Length; counter++)
- {
- var query = db.Query();
- query.Constrain(typeof(SimpleObject));
- query.Descend("_id").Constrain(matchingIds[counter]);
- IObjectSet queryResult = query.Execute();
- if (queryResult.Count == 1)
- returnValue[counter] = (SimpleObject)queryResult[0];
- }
- return returnValue;
- }
- GetAll: 2450 ms
- GetWithOriginalCode: 2694 ms
- GetWithSODAandProperty: 75373 ms
- GetWithSODAandField: 77 ms
- from SimpleObject simple in db
- where matchingIds.Contains(simple.Id)
- select simple
- var result1 = db.ObjectByID((SimpleObject t) => t.Id, 42, 77);
- var result2 = db.ObjectByID((SimpleObject t) => t.Id, myIDList);
- var result3 = db.ObjectByID((OtherObject t) => t.Name, "gamlerhart","db4o");
- var result = from SimpleObject t
- where t.Id = 42 || t.Id==77 ... t.Id == N
- select t
- public static class ContainerExtensions{
- public static IDb4oLinqQuery<TObjectType> ObjectByID<TObjectType, TIdType>(this IObjectContainer db,
- Expression<Func<TObjectType, TIdType>> idPath,
- params TIdType[] ids)
- {
- if(0==ids.Length)
- {
- return db.Cast<TObjectType>().Where(o=>false);
- }
- var orCondition = BuildOrChain(ids, idPath);
- var whereClause = Expression.Lambda(orCondition, idPath.Parameters.ToArray());
- return db.Cast<TObjectType>().Where((Expression<Func<TObjectType, bool>>) whereClause);
- }
- private static BinaryExpression BuildOrChain<TIdType, TObjectType>(TIdType[] ids, Expression<Func<TObjectType, TIdType>> idPath)
- {
- var body = idPath.Body;
- var currentExpression = Expression.Equal(body, Expression.Constant(ids.First()));
- foreach (var id in ids.Skip(1))
- {
- currentExpression = Expression.OrElse(currentExpression, Expression.Equal(body, Expression.Constant(id)));
- }
- return currentExpression;
- }
- }
Add Comment
Please, Sign In to add comment