我正在嘗試爲我的實體編寫一些通用的LINQ查詢,但我在做更復雜的事情時遇到了問題。現在,我使用了我所有的仿製藥的EntityDao類和我的每一個對象類的DAO(如成績DAO)繼承它,是例如:LINQ與繼承基類的通用查詢?
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做到這一點?
編輯:
getOne似乎沒有工作,即使只是要關閉的基類(這是一個局部類自動生成的LINQ類)。我甚至刪除了仿製藥。我想:
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'.
=(
嗯,無論如何要做到這一點,而不通過函數調用的謂詞?這是我真正想避免的。 主要問題是因爲我的DAO類沒有名爲ID的成員。我喜歡將我的對象類與數據庫邏輯類(DAO)和業務邏輯類(服務)分開。 這似乎很奇怪。是否沒有表達式或.command來表示將字段名稱/屬性輸入爲字符串? – SventoryMang 2010-04-08 02:12:23
其實這一點是你的謂詞可以是範圍縮小到單個實體的任何東西。例如x => x.name ==「some Name」&& x.created ==「02/03/2010」只要它返回一個實體就會對謂詞有效。如果你想單獨的DAO和服務類,那麼你將不得不在任何情況下翻譯邏輯。如果你想使用列名和值,我不知道你會怎麼做。 – 2010-04-08 02:35:11
是否有可能建立一個斷言字符串參數?我會猜測沒有,因爲x.Id中的id是已知屬性。 如果可能的話,那麼我想這只是一個兩步的過程,從所提供的參數中構建一個謂詞,然後將這個謂詞放到上面的示例中。 – SventoryMang 2010-04-08 02:47:35