Skip to content

Latest commit

 

History

History
33 lines (20 loc) · 1.61 KB

customizing_linq.md

File metadata and controls

33 lines (20 loc) · 1.61 KB
The Linq parsing and translation to Postgresql JSONB queries, not to mention Marten's own helpers and model, are pretty involved and this guide isn't exhaustive. Please feel free to ask for help in Marten's Gitter room linked above if there's any Linq customization or extension that you need.

Marten allows you to add Linq parsing and querying support for your own custom methods. Using the (admittedly contrived) example from Marten's tests, say that you want to reuse a small part of a Where() clause across different queries for "IsBlue()." First, write the method you want to be recognized by Marten's Linq support:

<[sample:IsBlue]>

Note a couple things here:

  1. If you're only using the method for Linq queries, it technically doesn't have to be implemented and never actually runs
  2. The methods do not have to be extension methods, but we're guessing that will be the most common usage of this

Now, to create a custom Linq parser for the IsBlue() method, you need to create a custom implementation of the IMethodCallParser interface shown below:

<[sample:IMethodCallParser]>

The IMethodCallParser interface needs to match on method expressions that it could parse, and be able to turn the Linq expression into part of a Postgresql "where" clause. The custom Linq parser for IsBlue() is shown below:

<[sample:custom-extension-for-linq]>

Lastly, to plug in our new parser, we can add that to the StoreOptions object that we use to bootstrap a new DocumentStore as shown below:

<[sample:using_custom_linq_parser]>