【发布时间】:2010-04-07 20:57:00
【问题描述】:
我正在尝试为我的实体编写一些通用 LINQ 查询,但是在执行更复杂的事情时遇到了问题。现在我正在使用一个 EntityDao 类,它具有我所有的泛型,并且我的每个对象类 Daos(例如 Accomplishments Dao)都继承它,例如:
using LCFVB.ObjectsNS;
using LCFVB.EntityNS;
namespace AccomplishmentNS
{
public class AccomplishmentDao : EntityDao<Accomplishment>{}
}
现在我的 entityDao 有以下代码:
using LCFVB.ObjectsNS;
using LCFVB.LinqDataContextNS;
namespace EntityNS
{
public abstract class EntityDao<ImplementationType> where ImplementationType : Entity
{
public ImplementationType getOneByValueOfProperty(string getProperty, object getValue)
{
ImplementationType entity = null;
if (getProperty != null && getValue != null) {
//Nhibernate Example:
//ImplementationType entity = default(ImplementationType);
//entity = Me.session.CreateCriteria(Of ImplementationType)().Add(Expression.Eq(getProperty, getValue)).UniqueResult(Of InterfaceType)()
LCFDataContext lcfdatacontext = new LCFDataContext();
//Generic LINQ Query Here
lcfdatacontext.GetTable<ImplementationType>();
lcfdatacontext.SubmitChanges();
lcfdatacontext.Dispose();
}
return entity;
}
public bool insertRow(ImplementationType entity)
{
if (entity != null) {
//Nhibernate Example:
//Me.session.Save(entity, entity.Id)
//Me.session.Flush()
LCFDataContext lcfdatacontext = new LCFDataContext();
//Generic LINQ Query Here
lcfdatacontext.GetTable<ImplementationType>().InsertOnSubmit(entity);
lcfdatacontext.SubmitChanges();
lcfdatacontext.Dispose();
return true;
}
else {
return false;
}
}
}
}
我已经让 insertRow 函数工作了,但是我什至不知道如何去做 getOnebyValueOfProperty,我在这个网站上能找到的最接近的东西是:
如何使用我当前的设置传递列名和我要检查的值?从那个链接看来,这是不可能的,因为使用 where 谓词,因为实体类在我传入它们之前不知道任何属性是什么。
最后,我需要某种方法将新对象设置为设置为实现类型的返回类型,在 nhibernate 中(我试图从它转换)它只是这行:
ImplentationType entity = default(ImplentationType);
但是默认是 nhibernate 命令,我将如何为 LINQ 执行此操作?
编辑:
即使只是离开基类(这是自动生成的 LINQ 类的部分类),getOne 似乎也不起作用。我什至删除了泛型。我试过了:
namespace ObjectsNS
{
public partial class Accomplishment
{
public Accomplishment getOneByWhereClause(Expression<Action<Accomplishment, bool>> singleOrDefaultClause)
{
Accomplishment entity = new Accomplishment();
if (singleOrDefaultClause != null) {
LCFDataContext lcfdatacontext = new LCFDataContext();
//Generic LINQ Query Here
entity = lcfdatacontext.Accomplishments.SingleOrDefault(singleOrDefaultClause);
lcfdatacontext.Dispose();
}
return entity;
}
}
}
得到以下错误:
Error 1 Overload resolution failed because no accessible 'SingleOrDefault' can be called with these arguments:
Extension method 'Public Function SingleOrDefault(predicate As System.Linq.Expressions.Expression(Of System.Func(Of Accomplishment, Boolean))) As Accomplishment' defined in 'System.Linq.Queryable': Value of type 'System.Action(Of System.Func(Of LCFVB.ObjectsNS.Accomplishment, Boolean))' cannot be converted to 'System.Linq.Expressions.Expression(Of System.Func(Of LCFVB.ObjectsNS.Accomplishment, Boolean))'.
Extension method 'Public Function SingleOrDefault(predicate As System.Func(Of Accomplishment, Boolean)) As Accomplishment' defined in 'System.Linq.Enumerable': Value of type 'System.Action(Of System.Func(Of LCFVB.ObjectsNS.Accomplishment, Boolean))' cannot be converted to 'System.Func(Of LCFVB.ObjectsNS.Accomplishment, Boolean)'. 14 LCF
好的没问题我改了:
public Accomplishment getOneByWhereClause(Expression<Action<Accomplishment, bool>> singleOrDefaultClause)
到:
public Accomplishment getOneByWhereClause(Expression<Func<Accomplishment, bool>> singleOrDefaultClause)
错误消失。好的,但是现在当我尝试通过以下方式调用该方法时:
Accomplishment accomplishment = new Accomplishment();
var result = accomplishment.getOneByWhereClause(x=>x.Id = 4)
它说 x 没有被声明是行不通的。
我也试过了
getOne<Accomplishment>
Expression<Func<
Expression<Action<
各种格式,但要么参数没有被正确识别为函数调用中的表达式,要么无法将我作为参数的类型转换为singleofDefault()中使用的类型。所以这两个错误就像上面一样。而且班级成绩确实有ID。最后,我还尝试将 x 声明为一项新成就,以便将其声明,此时代码会自动将 => 更改为 >= 并说:
Error 1 Operator '>=' is not defined for types 'LCFVB.ObjectsNS.Accomplishment' and 'Integer'.
=(
【问题讨论】:
标签: c# .net linq linq-to-sql generics