0

I want to be able to find a TheType that contains an arrayOfArraysOfIds that has any element of the list I provide. The thing is when I do the query down bellow, it doesn't return anything. What is the right format or what function should I use instead?

public class TheType
{
    public IList<IList<string>> arrayOfArraysOfIds;
}

theCollection.AsQueryable<theType>().
    Where(t => t.arrayOfArraysOfIds.ContainsAny(listOfIds));

The listOfIds is of type IEnumerable<IList<string>> as required by the ContainsAny function and it is constructed from a List as described here.

The problem is that the query doesn't return anything even if there's an element in the DB that should be returned.

Community
  • 1
  • 1
user3842408
  • 89
  • 1
  • 10
  • You haven't shown the definition of `ContainsAny`. And of course I wouldn't expect any query provider to know how to handle it; I'd expect it to throw a not supported exception. – Servy Apr 02 '15 at 17:21
  • 1
    It is a MongoDB.Driver.Linq function. http://docs.mongodb.org/ecosystem/tutorial/use-linq-queries-with-csharp-driver/ ContainsAny (LINQ to MongoDB extension method) This method is used to test whether an array (or array-like) field or property contains any of the provided values. – user3842408 Apr 02 '15 at 17:25
  • Is listOfIds a List? – Praveen Paulose Apr 02 '15 at 17:29
  • listOfIds is of type IEnumerable> as required by the ContainsAny function. Since arrayOfArraysOfIds is a IList> – user3842408 Apr 02 '15 at 18:00
  • One other thing you might want to think about...are you testing whether one of arrayOfArraysOfIds's pointers is pointing to the same location as one of the pointers in the listOfIds, or are you just looking to see if they have arrays in common that contain the same set of string values? I'm not sure how the Contains method would evaluate the condition. – Francine DeGrood Taylor Apr 02 '15 at 19:53
  • ArrayOfArraysOfIds is a mongodb field. It is not generated by me. – user3842408 Apr 02 '15 at 21:24

3 Answers3

0

One way that you could gain more control, as well as visibility into why you weren't getting any values out would be to explicitly define your own ContainsAny method so you could go in with a debugger. I've found this practice, though not as cleverly elegant as using a complex LINQ command, saved me a lot of hair pulling.

    public class TheType
    {
        public IList<IList<string>> arrayOfArraysOfIds;
        public bool ContainsAny(IList<string> listOfIds)
        {
            return arrayOfArraysOfIds.Contains(listOfIds);
        }
    }

   theCollection.AsQueryable<TheType>().Where(t => t.ContainsAny(listOfIds));
0

The solution is to make a query to MongoDB and not ask breeze to interpret it since the query is directly supported by MongoDB..

theCollection.Find(Query.ElemMatch("listOfIds", Query.In("$elemMatch", new BsonArray(listOfIds)))).AsQueryable();
user3842408
  • 89
  • 1
  • 10
-1

Try this:

theCollection.AsQueryable<TheType>().Where(t => t.arrayOfArraysOfIds.Contains(listOfIds));

...where listOfIds is defined as a generic list

IList<string> listOfIds = new List<string>();