extending the QueryBuilder of MongoDB C # driver
Because you do not want to directly hardcode the string "ClassA.MemberA.MemberB", write the following classes for the following common scenarios:
1. Expression converted to String function: Exptostr ()
2. Collection function: When you have a member of a collection, you can use this class to return the Querycollection object, which is appended with the code for this class.
3. Collectionas function: When inheritance is used, you want to convert the base class to a subclass and return the querycollection of the subclass
Examples of Use:
Gets the string form of an expression 1. Queryex<classa>. Exptostr ((ClassA m) = M.MEMBERA.MEMBERB.MEMBERC)//collection. member. Field//poppedsegments is a collection, Assignednetwork.name is a member// will return PoppedSegments.AssignedNetwork.Name2. Queryex<mddelivery>. Collection (x = x.poppedsegments). Matches (p = p.assignednetwork.name, Bsonregex),//Subclass collection. member. Field//stpaymenttransaction is the base class, Stpaymentcompanycredit is a subclass, the company field in the subclass//will return PAYMENTS.COMPANY.NAME3. Queryex<mddelivery>. Collectionas<stpaymenttransaction, Stpaymentcompanycredit> (x=>x.payments). Matches (P=>p.company.name, Bsonregex)//collection. member. Field//parcels is a collection, Stcustompropertyruntime is a base class, Stnumericpropertyruntime is a subclass, Customprops is a member of Stnumericpropertyruntime, value is a member of Customprop// will return Parcels.CustomProps.Value4. Queryex<mddelivery>. Collection (X=>x.parcels). Collectionmemberas<stcustompropertyruntime, Stnumericpropertyruntime> (p=>p.customprops). Matches (P=>p.value, Bsonregex),
Implementation code:
public class Queryex<tdocument> {public static querycollection<tdocument, tcollection> COLLECTION&L T Tcollection> (expression<func<tdocument, ienumerable<tcollection>>> collectionExpression) {return new querycollection<tdocument, tcollection> (collectionexpression); }//for those cases using inheritance//e.g stpaymenttransaction//payments would return Stpaymenttran Saction//need to cast to sub classes (Stpaymentcompanycredit) so is able to filter by the child members (e.g. Com pany) public static querycollection<tdocument, tsub> collectionas<tcollection, tsub> (Expres Sion<func<tdocument, ienumerable<tcollection>>> collectionexpression) where tsub:tcollectio n {var argparam = Expression.parameter (typeof (Tdocument), "X"); var memberstr = exptostr (collectionexpression); MembeRexpression NameProperty = Expression.property (Argparam, MEMBERSTR); var subexp = Expression.convert (NameProperty, typeof (Ienumerable<tsub>)); var exp = expression.lambda<func<tdocument, ienumerable<tsub>>> (Subexp, a Rgparam); return new querycollection<tdocument, tsub> (exp); }//<summary>//Return string value for a expression:///For S.name.val1.val2 would return Name.Val1.Val2//</summary>//<typeparam name= "Mdclass" ></typeparam>//<ty Peparam name= "Member" ></typeparam>//<param name= "exp" ></param>//<RETURNS>&L t;/returns> public static string exptostr<tdocument, Member> (Expression<func<tdocument, MEMBER>&G T EXP) {return new Queryexpressionhelper (). Memberexpression (exp); }}public Class Querycollection<tdocUment, tcollection> {private readonly queryexpressionhelper _queryexpression; private string _collectionname; public string Context {get {return _collectionname;} } public Querycollection (Expression<func<tdocument, ienumerable<tcollection>>> collectionExpress ION) {_queryexpression = new queryexpressionhelper (); _collectionname = _queryexpression.memberexpression (collectionexpression); } public Querymember<tcollection, Tmember> member<tmember> (expression<func<tcollection, TMEMBER&G T;> exp) {var expstr = Queryex<tcollection>. EXPTOSTR (exp); var context = string. Format ("{0}.{ 1} ", _collectionname, EXPSTR); var obj = new Querymember<tcollection, tmember> (context); return obj; } public Querycollection<tcollection, Tmember> collectionmember<tmember> (Expression<func<tcollection, ienumerable<tmember>>> exp) {var expstr = Queryex<tco Llection>. EXPTOSTR (exp); var obj = new querycollection<tcollection, tmember> (exp) {_collectionname = string. Format ("{0}.{ 1} ", _collectionname, Expstr)}; return obj; }///<summary>//This method is only a support 1 layer nested (not for Query collection.collection, but For collection.member)///If Member is Collection and need convert to sub class///</summary> <typeparam name= "Tmember" >base type</typeparam>//<typeparam name= "Tmembersub" >Child Clas s type</typeparam>//<param name= "collectionexpression" ></param>///<returns>< /returns> public querycollection<tcollection, tmembersub> Collectionmemberas<tmember, TMemberSub> ( Expression<fuNc<tcollection, ienumerable<tmember>>> collectionexpression) where Tmembersub:tmember { var obj = Queryex<tcollection>. Collectionas<tmember, tmembersub> (collectionexpression); Obj._collectionname = string. Format ("{0}.{ 1} ", _collectionname, Obj._collectionname); return obj; } public Imongoquery lt<tmember> (expression<func<tcollection, tmember>> memberExpression, TMember Value) {var membername = _queryexpression.memberexpression (memberexpression); Return query.lt (String. Format ("{0}.{ 1} ", _collectionname, MemberName), bsonvalue.create (value)); } public Imongoquery lt<tvalue> (expression<func<tcollection, ienumerable<tvalue>>> memberEx Pression, TValue value) {var membername = _queryexpression.memberexpression (memberexpression); Return query.lt (String. Format ("{0}.{ 1} ", _collectionname, MemberName), Bsonvalue.create (value)); } public Imongoquery eq<tmember> (expression<func<tcollection, tmember>> memberExpression, TMember Value) {var membername = _queryexpression.memberexpression (memberexpression); Return Query.eq (String. Format ("{0}.{ 1} ", _collectionname, MemberName), bsonvalue.create (value)); } public Imongoquery eq<tvalue> (expression<func<tcollection, ienumerable<tvalue>>> memberEx Pression, TValue value) {var membername = _queryexpression.memberexpression (memberexpression); Return Query.eq (String. Format ("{0}.{ 1} ", _collectionname, MemberName), bsonvalue.create (value)); } public Imongoquery ne<tmember> (expression<func<tcollection, tmember>> memberExpression, TMember Value) {var membername = _queryexpression.memberexpression (memberexpression); Return Query.ne (String. Format ("{0}.{1} ", _collectionname, MemberName), bsonvalue.create (value)); } public Imongoquery ne<tvalue> (expression<func<tcollection, ienumerable<tvalue>>> memberEx Pression, TValue value) {var membername = _queryexpression.memberexpression (memberexpression); Return Query.ne (String. Format ("{0}.{ 1} ", _collectionname, MemberName), bsonvalue.create (value)); } public Imongoquery in<tmember> (expression<func<tcollection, tmember>> memberExpression, params Tmember[] values) {return in<tmember> (memberexpression, new list<tmember> (values)); } public Imongoquery in<tmember> (expression<func<tcollection, tmember>> memberExpression, Ienumerable<tmember> values) {var membername = _queryexpression.memberexpression (memberexpre Ssion); Return Query.in (String. Format ("{0}.{ 1} ", _collectionname, MemberName), ValuEs. Select (x = bsonvalue.create (x))); } public Imongoquery IN<TCASTC, tmember> (EXPRESSION<FUNC<TCASTC, tmember>> memberExpression, ienumerable<tmember> values) where tcastc:tcollection {var membername = _queryexpression . Memberexpression (memberexpression); Return Query.in (String. Format ("{0}.{ 1} ", _collectionname, MemberName), values. Select (x = bsonvalue.create (x))); } public Imongoquery in<tvalue> (expression<func<tcollection, ienumerable<tvalue>>> memberEx pression, ienumerable<tvalue> values) {var membername = _queryexpression.memberexpression (MemberE Xpression); Return Query.in (String. Format ("{0}.{ 1} ", _collectionname, MemberName), values. Select (x = bsonvalue.create (x))); } public Imongoquery IN<TCASTC, tvalue> (EXPRESSION<FUNC<TCASTC, ienumerable<tvalue>>> Membe Rexpression, ienumerable&Lt tvalue> values) where tcastc:tcollection {var membername = _queryexpression.memberexpression (Membe Rexpression); Return Query.in (String. Format ("{0}.{ 1} ", _collectionname, MemberName), values. Select (x = bsonvalue.create (x))); } public Imongoquery matches<tmember> (expression<func<tcollection, tmember>> memberExpression, Bs Onregularexpression value) {var membername = _queryexpression.memberexpression (memberexpression); Return Query.matches (String. Format ("{0}.{ 1} ", _collectionname, MemberName), value); } public Imongoquery matches<tvalue> (expression<func<tcollection, ienumerable<tvalue>>> mem Berexpression, bsonregularexpression value) {var membername = _queryexpression.memberexpression (MemberE Xpression); Return Query.matches (String. Format ("{0}.{ 1} ", _collectionname, MemberName), value); }}public Class QuerymembEr<tdocument, tcollection> {private readonly queryexpressionhelper _queryexpression; private string _collectionname; public string Context {get {return _collectionname;} } public Querymember (Expression<func<tdocument, tcollection>> exp) {_queryexpression = new Queryexpressionhelper (); _collectionname = _queryexpression.memberexpression (exp); Public Querymember (String context) {_collectionname = context; }}public class Queryexpressionhelper {public string Context; public string memberexpression<tmember> (expression<tmember> Expression) {memberexpression m E switch (expression. Body.nodetype) {Case ExpressionType.MemberAccess:me = expression. Body as Memberexpression; Break Case Expressiontype.converT:dynamic convertedbody = expression. Body; me = Convertedbody.operand as memberexpression; Break Default:throw new NotSupportedException (string. Format ("Member with node type ' {0} is not supported. Expression {1} ", expression. Body.nodetype, expression)); } var stack = new stack<string> (); while (me = null) {stack. Push (Me. Member.name); me = Me. Expression as Memberexpression; } var expstr = string. Join (".", Stack. ToArray ()); return expstr; }}public Static class Querymoney {public static imongoquery Value (string name, double val) { var accuracy = 0.005; Return Query.and (query.lt (name, new bsondouble (val + accuracy)), QUERY.GT (name, new Bsondou BLE (val-accuracy)); } }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Extending the QueryBuilder of MongoDB C # driver