LiteDB icon indicating copy to clipboard operation
LiteDB copied to clipboard

[SUGGESTION] Allow Method groups as a substitute for an BsonExpression

Open agaertner opened this issue 3 years ago • 2 comments

Is your feature request related to a problem? Please describe. Code gets bulky and inability to use method groups as a substitute for expressions causes duplicated expressions ie. code fragments.

Describe the solution you'd like Overload that translated my static method group

db.FindOne(MyModel.IsValid)

to a BsonExpression equivalent which is:

db.FindOne(x => x.Foobar && x.Arbitrary > 0)

where MyClass.IsValid in the example is defined as follows.

public class MyModel {
    public bool Foobar { get; set; }
    public long Arbitrary { get; set; }
    public static bool IsValid(MyModel model) => model.Foobar && model.Arbitrary > 0;
}

Describe alternatives you've considered Dublicated code fragments (expressions), but code gets bulky (see above) because dublicated expression cannot be managed from a single place and are spread across the project.

agaertner avatar May 16 '22 17:05 agaertner

there is 2 way to do that ,

public class MyModel {
    public bool Foobar { get; set; }
    public long Arbitrary { get; set; }

   //property style -- with this , you dont have to pass model to function.
   // its already accesing instances fields
    public bool IsValid => Foobar && Arbitrary > 0;

   //method style  -- you have to pass model,  
    public static bool isValid_method(MyModel item ) { return item.Foobar && item.Arbitrary > 0;  }
}

usage:

    //property style
    var resultsxx = col.Find(x => x.isValid ) ;
    
   //method style
    var resultsx2 = col.Find(x => MyModel .isValid_method( x)     ) ;

i didnt encount any syntax error ...

blackholeearth avatar Jun 13 '22 08:06 blackholeearth

Of course. But this is not the same syntactic sugar I am requesting. I specifically request to have method groups be passable without x => MyModel.IsValid(x) when the Find method is expecting a single parametrized method. LINQ normally supports it.

agaertner avatar Jun 14 '22 18:06 agaertner