Linq 在Cassandra db上创建泛型类方法更新
检查cassandra c#driver()上datastax的文档,显示以下代码:Linq 在Cassandra db上创建泛型类方法更新,linq,generics,c#-4.0,cassandra-2.0,Linq,Generics,C# 4.0,Cassandra 2.0,检查cassandra c#driver()上datastax的文档,显示以下代码: // Update users.Where(u => u.UserId == "john") .Select(u => new User { LastAccess = TimeUuid.NewId()}) .Update() .Execute(); 我决定创建这样一个存储库: public bool Update(Func<T,bool> whe
// 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;
}
}
public bool更新(Func-whereExpression,Func-selectExpression)
{
尝试
{
这是所有的记录
.Where(whereExpression)
.选择(选择表达式)
.Update()
.Execute();
}
捕获(例外)
{
投掷;
}
}
我收到了以下错误:
错误1“System.Collections.Generic.IEnumerable”不包含“Update”的定义,并且找不到接受“System.Collections.Generic.IEnumerable”类型的第一个参数的扩展方法“Update”(是否缺少using指令或程序集引用?)
我已经导入了所有必要的名称空间
使用System.Linq;
使用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;