C# 限制。在从作用域“”引用的类型为“Product”的(_func)-变量“p”上,但未在传递上定义该变量
我一直无法使这个通用的nhibernate查询正常工作,因为我不断得到: 从范围引用了类型为“Product”的变量“p”,但未定义该变量 对于这个问题,我已经尝试了大量不同的答案,我想我理解问题的所在,只是不知道如何解决它。据我所知,“p”存在于原始作用域中,但在使用nhibernate queryover命令时停止存在,因此,由于Linq按名称引用对象,它实际上不再知道我在说什么 我尝试了各种方法,包括: ; ; ; ; 不幸的是,这些都没有提供一个能够解决这个问题的答案。查询:C# 限制。在从作用域“”引用的类型为“Product”的(_func)-变量“p”上,但未在传递上定义该变量,c#,linq,nhibernate,generics,linq-to-nhibernate,C#,Linq,Nhibernate,Generics,Linq To Nhibernate,我一直无法使这个通用的nhibernate查询正常工作,因为我不断得到: 从范围引用了类型为“Product”的变量“p”,但未定义该变量 对于这个问题,我已经尝试了大量不同的答案,我想我理解问题的所在,只是不知道如何解决它。据我所知,“p”存在于原始作用域中,但在使用nhibernate queryover命令时停止存在,因此,由于Linq按名称引用对象,它实际上不再知道我在说什么 我尝试了各种方法,包括: ; ; ; ; 不幸的是,这些都没有提供一个能够解决这个问题的答案。查询: publ
public class ProductsInformationQuery<T>: Query<T> where T:Product
{
private readonly List<string> _ids;
private readonly Expression<Func<T, object>> _func;
public ProductsInformationQuery(List<string> ids,
FeaturedIdType featureIdType, Expression<System.Func<T,object>> func)
{
_ids = ids;
_func = func;
}
public override List<T> Execute()
{
return Session.QueryOver<T>().Where(Restrictions.On<T>(_func).IsIn(_ids.Select(i => i).ToList())).List().ToList();
}
}
这将接收字符串列表并检查数据库中的特定查询。这将允许我们尝试检查数据库中的一组ID;Id可以是GUID(如Id)或字符串(如名称)
呼叫代码:
private List<Product> LoadProducts(List<string> ids)
{
System.Linq.Expressions.Expression<Func<Product, object>> function = b => b.Id.ToString();
return _productsRepository.Execute(new ProductsInformationQuery<Product>(ids, FeaturedIdType.ProductId,function)).ToList();
}
此代码需要能够将'b=>b.Id.ToString'作为需要检查的函数发送进来。然后,QueryOver命令需要根据其Id查找产品。产品是继承链的顶层,因此我希望能够通过数据库中的各种信息查找各种类型的产品
有趣的是,我可以做到以下几点:
return Session.QueryOver<T>().Where(Restrictions.On<T>(b=>b.Id).IsIn(_ids.Select(Guid.Parse).ToList())).List().ToList();
但不是以下两个:
return Session.QueryOver<T>().Where(Restrictions.On<T>(b=>b.Id).IsIn(_ids)).List().ToList();
AOD例外
return Session.QueryOver<T>().Where(Restrictions.On<T>(b=>b.Id.ToString()).IsIn(_ids)).List().ToList();
上述误差;类型为的变量“b”
前者是Guid到Guid的比较,第二个是Guid到String,这并不奇怪Guid到String不能与nhibernate一起使用,第三个是Guid到String到String,这将改变b,而不仅仅是收集数据
有没有办法让它接受字符串、guid、int和任何其他数据类型,同时还能接受任何类型的产品?还是我只限于对查询进行几个版本
谢谢 我将向类中添加一个泛型类型参数TIdType,该参数表示要使用的Id的类型。结果会是这样的:
public class ProductsInformationQuery<TResultType, TIdType>: Query<TResultType>
where TResultType : Product
{
private readonly List<TIdType> _ids;
private readonly Expression<Func<TResultType, object>> _func;
public ProductsInformationQuery(
List<TIdType> ids,
FeaturedIdType featureIdType,
Expression<System.Func<TResultType, TIdType>> func)
{
_ids = ids;
System.Linq.Expressions.Expression converted =
System.Linq.Expressions.Expression.Convert(func.Body, typeof(object));
_func = System.Linq.Expressions.Expression.Lambda<Func<TResultType, object>>
(converted, func.Parameters);
}
public override List<TResultType> Execute()
{
return Session.QueryOver<TResultType>()
.Where(Restrictions.On<TResultType>(_func)
.IsIn(_ids.ToArray())
.List()
.ToList();
}
}
private List<Product> LoadProducts(List<string> ids)
{
System.Linq.Expressions.Expression<Func<Product, Guid>> function = b => b.Id;
return _productsRepository.Execute(
new ProductsInformationQuery<Product, Guid>
(ids, FeaturedIdType.ProductId, function))
.ToList();
}
我获取了将表达式转换为表达式的代码
您的呼叫代码如下所示:
public class ProductsInformationQuery<TResultType, TIdType>: Query<TResultType>
where TResultType : Product
{
private readonly List<TIdType> _ids;
private readonly Expression<Func<TResultType, object>> _func;
public ProductsInformationQuery(
List<TIdType> ids,
FeaturedIdType featureIdType,
Expression<System.Func<TResultType, TIdType>> func)
{
_ids = ids;
System.Linq.Expressions.Expression converted =
System.Linq.Expressions.Expression.Convert(func.Body, typeof(object));
_func = System.Linq.Expressions.Expression.Lambda<Func<TResultType, object>>
(converted, func.Parameters);
}
public override List<TResultType> Execute()
{
return Session.QueryOver<TResultType>()
.Where(Restrictions.On<TResultType>(_func)
.IsIn(_ids.ToArray())
.List()
.ToList();
}
}
private List<Product> LoadProducts(List<string> ids)
{
System.Linq.Expressions.Expression<Func<Product, Guid>> function = b => b.Id;
return _productsRepository.Execute(
new ProductsInformationQuery<Product, Guid>
(ids, FeaturedIdType.ProductId, function))
.ToList();
}