1

I'm hoping someone can help me:

I'm using an interface to access my data, the Find method is:

public IQueryable<T> Find<T>(Expression<Func<T, bool>> expression) where T : class, new()
    {
        return (IQueryable<T>) _db.GetCollection<T>(typeof(T).Name, WriteConcern.Acknowledged).AsQueryable().Where(expression);
    }

I'm trying to build an expression to fetch all ObjectIds within a list of ObjectIds.

something like: r => r.id.ContainsAny(List_Of_IDs); //where List_Of_IDs is of type: List

I also tried: r => r.id.ContainsAny(new[] {id1, id2, id3}) //where id1, id2, id3 are type ObjectId

But I get an error that ContainsAny doesn't support this parameter.

I was hoping to duplicate the functionality of an IN statement in SQL. My goal is that I want the user collection to contain a list of products as a list of ObjectIds and then I want to query the products collection and get all documents in the user's list of Product IDs.

Is this possible? Does anyone know what I'm doing wrong?

Thanks so much for any assistance! Steve

BTW: here is where I send the expression to the repository method:

public static List<product> getByIDlist(List<ObjectId> IDs)
    {
        return (List<product>)repo.Find<product>(r => r.id.ContainsAny(IDs));       
    }
4

1 回答 1

0

.ContainsAny()仅用于数组字段。. 因此,如果您.id是数组类型,则可以使用它。

对于您正在寻找的情况.Contains().In()

public static List<product> getByIDlist(List<ObjectId> IDs)
{
    return (List<product>) repo.Find<product>(r => IDs.Contains(r.id);       
}

巧合的是,这相当于:

public static List<product> getByIDlist(List<ObjectId> IDs)
{
    return (List<product>) repo.Find<product>(r => r.id.In(IDs));       
}
于 2013-05-10T13:12:51.797 回答