'C# MongoDb Driver Filter using nested property from collection
I have the following class structures :
public class QuoteRequestInfo
{
. . .
public LocationInfo LocationInfo {get;set;}
. . .
}
LocationInfo has a stop collection which will always contain 2 stops :
public class LocationInfo
{
. . .
public IEnumerable<Stop> Stops{get;set};
. . .
}
Stop has the following property :
public class Stop
{
. . .
public string StateName {get;set};
. . .
}
I am trying to search QuoteRquestInfos collection by filtering on multiple state names like so :
var states = new List<string> {"VA"}
var filter = Builders<QuoteRequestInfo>.Filter.In(r => r.LocationInfo.Stops.First().StateCode, states)
var quotes = await collection.Find(filter).ToListAsync();
This is returning an empty filter and not matching anything? how should I be passing in the expression to the In Filter ? any ideas ?
Solution 1:[1]
There are a lot of ways, for example:
var states = new List<string> { "VA" };
var quotes = coll.AsQueryable()
.Select(c =>
new {
Previous = new QuoteRequestInfo { LocationInfo = c.LocationInfo }, // this partocular line saves the previous document
StoredFirst = c.LocationInfo.Stops.First().StateName }) // save a flag about first stop since call it in the same construction with `Where` is not supported via typed way
.Where(f => states.Contains(f.StoredFirst))
.Select(c => c.Previous)
.ToList();
Solution 2:[2]
<= operator is used in comparing numbers: see operators
To compare symver, you may want other methods (or simply php -r 'version_compare("${{ some expr }}", "${{ some expr }}");')
I may use something like
steps:
- name: Check PR version
shell: bash
run: |
# you can also use node, python, php things
pr="$(jq -r '.version' < ./package.json)"
# get my latest version
# modify this with your own style
latest="$(git describe --tag --abbrev=0)"
php -r 'if(version_compare("'"$pr"'","'"$latest"'",">")){exit(0);};exit(1);'
- name: Step that wont run if not valid
shell: bash
run: |
echo balabala
or
steps:
- name: Check PR version
shell: bash
# set step name for futher use
id: check
run: |
# you can also use node, python, php things
pr="$(jq -r '.version' < ./package.json)"
# get my latest version
# modify this with your own style
latest="$(git describe --tag --abbrev=0)"
php -r 'echo"::set-output name=valid::".(version_compare("'"$pr"'","'"$latest"'",">")?"valid":"invalid").PHP_EOL;'
- name: Step that always run
shell: bash
run: |
echo "pr is ${{ steps.check.outputs.valid }}"
if [ "${{ steps.check.outputs.valid }}" = "valid" ]
then
echo "im valid"
else
echo "im invalid"
fi
- name: Step that run at valid
if: steps.check.outputs.valid == 'valid'
shell: bash
run: |
echo "pr is ${{ steps.check.outputs.valid }}"
- name: Step that run at invalid
if: steps.check.outputs.valid != 'valid'
shell: bash
run: |
echo "pr is ${{ steps.check.outputs.valid }}"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | dododo |
| Solution 2 | Yun Dou |
