IDocumentSession.Query<> returns null when searching for a single document #2718
Replies: 3 comments
-
Query for a single document should work fine. What is the value of slug which you are searching and the entry in db? It would help for you to provide some code with data for us to check. |
Beta Was this translation helpful? Give feedback.
-
This is the method: public async Task<Template?> GetBySlugAsync(string slug)
{
var templates = await _session.Query<Template>().ToListAsync();
if (templates != null) foreach (var item in templates) if (item.Slug == slug) return item;
// return await _session.Query<Template>().Where(x => x.Slug == slug).SingleOrDefaultAsync();
return default;
} This is value of "slug" that is being passed to the method: var slug = "my-document-template"; This is the entry in the database: {
"id": "57b5239b-171d-4200-9bd2-ed7951d76e72",
"title": "My Document Template",
"slug": "my-document-template",
"description": "a brief Document description",
"tags": [
"first tag",
"another tag"
]
} Retrieving the entire list and then matching on the slug returns the correct database entry. |
Beta Was this translation helpful? Give feedback.
-
@johnholliday Hey, check your JSON settings in Marten. Notice that "slug" is I'm guessing camel casing in your database. Are you explicitly setting your JSON casing to CamelCase with Marten? I'm moving this to a discussion, because it's extremely unlikely that this is anything but the JSON serialization settings. |
Beta Was this translation helpful? Give feedback.
-
The following code works but requires loading the entire collection.
I have a potentially very large collection of documents, so I need to retrieve the specific documents I'm looking for. However, the following code always returns null because the sequence contains no elements.
This also fails similarly.
Beta Was this translation helpful? Give feedback.
All reactions