0

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 ?

1 Answer 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();
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for you answer, Thic could work but I was specifically trying to find a solution with out converting the Collection to Queryable .
it doesn't matter. AsQueryable just calls Aggregate behind the scene. So you can replace the above Select on Aggregate.Project and Where on Aggregate.Match. As I can see, you can't use Find only because it doesn't support using Projection before filtering
If you need Find, you can simply provide a filter in raw MQL form like: var quotes = await collection.Find("{ whatever : 1 }").ToListAsync();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.