user2979432
user2979432

Reputation: 83

how to use the lowercase in linq using C# mongodb

I am trying to Create the linq query using lowercase that time its getting error in C# MongoDB That the error looks like {document}{Queue}.ToString().ToLower() is not supported.

Here is my Code

(from c in Collection.AsQueryable()
 where c["Projects"]["_id"] == ProjectId && c["Name"].ToString().ToLower() == Name.ToLower
 select c).ToList();

Thanks, Pari`

Upvotes: 2

Views: 2125

Answers (2)

swatsonpicken
swatsonpicken

Reputation: 883

This is all from a vague recollection of a problem I had years ago, and I haven't been able to verify any of this, but... ...I think this might work.

(from c in Collection.AsQueryable()
where where c["Projects"]["_id"] == ProjectId
    && c["Name"].Equals(Name, StringComparison.OrdinalIgnoreCase)
select c).ToList();

I think this query ensures the name case check is happening client-side. I believe your original query is asking MongoDB to do the case checking, but the query parser has no idea how to convert C# constructs like ToLower().

Upvotes: 0

BRAHIM Kamel
BRAHIM Kamel

Reputation: 13774

This because IQueryProvider does not recognise how to deal with chained methods try to drop ToString() Or use the follwing syntax because is implementend and interpreted in the mongodriver something like the following

MongoCollection.FindAsync(c=>c.Name.ToLower()==name.ToLower()) 

I'm sure this should work because if you take a look at PredicateTranslate.cs of mongodriver you will find the methods that mongo can interpret

Upvotes: 1

Related Questions