在Cassandra db上创建泛型类方法更新

时间:2015-12-09 10:07:38

标签: linq generics c#-4.0 cassandra-2.0

检查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;
        }
    }

我收到以下错误:

  

错误1&#39; System.Collections.Generic.IEnumerable&#39;不包含&#39;更新&#39;的定义没有扩展方法&#39;更新&#39;接受类型为'System.Collections.Generic.IEnumerable&#39;的第一个参数。可以找到(你错过了使用指令或程序集引用吗?)

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

  

使用System.Linq;   使用Cassandra.Data.Linq;

1 个答案:

答案 0 :(得分:1)

问题在于构造方法参数。如果检查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;

这就是你需要做的所有工作......