mbdavid / LiteDB

LiteDB - A .NET NoSQL Document Store in a single data file
http://www.litedb.org
MIT License
8.62k stars 1.25k forks source link

[QUESTION] Why cannot I use LINQ directly on an ILiteCollection<> object? #1909

Open Sparkles-Laurel opened 3 years ago

Sparkles-Laurel commented 3 years ago

I was trying to query an ILiteCollection<> object (reference may be more suitable) with LINQ, as how I could just query an IEnumerable<>:

            var results = (from guild_ in RegisteredGuilds
                            where guild_.Guild == guild
                            select guild_.Guild).SingleOrDefault();
``` where RegisteredGuilds was```cs
public ILiteCollection<AvoidConfusion.Entities.RegisteredGuild> RegisteredGuilds{get; set;}

and this caused the compilation error below:

CS1936: Could not find an implementation of the query pattern for source type 'ILiteCollection<RegisteredGuild>'. 'Where' not found.

Why cannot I use LINQ queries directly on an ILiteCollection?

loanburger commented 3 years ago

Seems strange. I use LINQ as follows:

var document= _database.GetCollection<T>();
return document.Find(predicate);

where a predicate would be anything from `c=> c.customerId == '1234'

I have a fucntion like this:


/// <summary>
/// Get All items by expression
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="predicate"></param>
/// <returns></returns>
public IEnumerable<T> GetItems<T>(Expression<Func<T, bool>> predicate = null) where T : DbItem, new()
{
        var document = _db.GetCollection<T>();  
    return document.Find(predicate);            
}
Sparkles-Laurel commented 3 years ago

I just wondered why I couldn't use the query syntax on an ILiteCollection. (LINQ provides two query synatxes: query syntax and method syntax.)

Sparkles-Laurel commented 3 years ago

I think this is related to that LiteDB is a NoSQL database.