在 Cassandra 数据库上创建通用 class 方法更新

Creating a generic class method update on Cassandra db

检查 cassandra c# 驱动程序 (cassandra C# driver documentation) 上 datastax 的文档,显示了以下代码:

// Update 
users.Where(u => u.UserId == "john")
     .Select(u => new User { LastAccess = TimeUuid.NewId()})
     .Update()
     .Execute();

我决定像这样创建一个存储库:

    public bool Update(Func<T,bool> whereExpression,Func<T,T> selectExpression)
    {
        try
        {
            this.AllRecords
                .Where(whereExpression)
                .Select(selectExpression)
                .Update()
                .Execute();

        }
        catch (Exception)
        {

            throw;
        }
    }

我收到以下错误:

Error 1 'System.Collections.Generic.IEnumerable' does not contain a definition for 'Update' and no extension method 'Update' accepting a first argument of type 'System.Collections.Generic.IEnumerable' could be found (are you missing a using directive or an assembly reference?)

我已经导入了所有必要的命名空间

using System.Linq; using Cassandra.Data.Linq;

问题出在你的方法参数的构造上。如果您检查 Cassandra.Data.Linq 命名空间,Where 扩展方法将显示为:

public static CqlQuery<TSource> Where<TSource>(this CqlQuery<TSource> source, Expression<Func<TSource, bool>> predicate);

因此,您应该将代码更新为:

 public bool Update(Expression<Func<T,bool>> whereExpression, Expression<Func<T,T>> selectExpression)
{
    try
    {
        this.AllRecords
            .Where(whereExpression)
            .Select(selectExpression)
            .Update()
            .Execute();

    }
    catch (Exception)
    {

        throw;
    }
}

此外,不要忘记导入以下命名空间以使其正常工作:

using System.Linq;    
using System.Linq.Expressions;
using Cassandra.Data.Linq;

这就是您需要做的全部工作...