Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq List Contains

Tags:

c#

linq

arangodb

I am using ArangoDB and I am querying a collection named movies. Its data structure is such that categories is a List of strings.

public class movies
{
    [DocumentProperty(Identifier = IdentifierType.Key)]
    public string Key;
    public List<string> categories;
    public string desc;
    public string img;
    public List<vod_stream> streams;
    public string title;
};

Here is the query statement:

var result = db.Query<movies>()
            .Where(p => p.categories.Contains(id));

id is passed as a param and I need to retrieve all the movies that has the category matched by the id. However, above code doesn't work as result gives me ALL the movies in the collection.

foreach (movies mov in result)
{
    if (mov.categories.Contains(id) == false)
    { continue; }

    // do something here
}

The weird thing is when I loop through the items in the result, the same function does return false for some of the items. But it just doesn't work in the Linq statement.

Anybody knows what is wrong with my query statement?

like image 660
Darren Avatar asked Sep 12 '17 08:09

Darren


People also ask

How to check Contains in LINQ?

The Linq Contains Method in C# is used to check whether a sequence or collection (i.e. data source) contains a specified element or not. If the data source contains the specified element, then it returns true else return false.

How use contains in LINQ query?

To check for an element in a string, use the Contains() method. The following is our string array. string[] arr = { "Java", "C++", "Python"}; Now, use Contains() method to find a specific string in the string array.

How do you check if a string contains an element from a list in C#?

List<T>. Contains(T) Method is used to check whether an element is in the List<T> or not.

How do you use LINQ to check if a list of strings contains any string in a list?

Select(x => new { x, count = x. tags. Count(tag => list. Contains(tag)) }) .


1 Answers

For using AQL Functions in ArangoDB LINQ provider you should use ArangoDB.Client.AQL static methods.

For your use-case you can use in AQL function:

var result = db.Query<movies>()
                .Where(p => AQL.In(id, p.categories))
                .ToList();

which translates to :

for `p` in `movies`  filter  @P1 in `p`.`categories`  return   `p`

Built-in methods for c# data structures like List are not currently supported in ArangoDB LINQ provider

like image 188
raoof hojat Avatar answered Sep 19 '22 10:09

raoof hojat