2

サービスにオブジェクトを送信してデータベースを作成および更新し、サービスがデータベースに書き込みまたは更新するオブジェクトの種類を動的に決定できるようにしたいと考えています。

これは、静的に行うときに行うことです(更新の例)

switch ((string)property.GetValue(oLinq, null))
                {
                    case "news":
                        t_news oNews = (t_news)oLinq;                            
                        List<t_news> newsList = (from n in oDB.t_news where n.ID.Equals(oNews.ID) select n).ToList();
                        t_news oNe = newsList[0];
                        i = 0;
                        foreach (System.Reflection.PropertyInfo p in oNews.GetType().GetProperties().ToList())
                        {
                            if (p.GetValue(oNews, null) != null)
                            {
                                typeof(t_news).GetProperties().ToList()[i].SetValue(oNe, p.GetValue(oNews, null), null);
                            }
                            i++;
                        }
                        oNe.dLastUpdate = DateTime.Now;
                        oDB.SubmitChanges();
                        oLinq = (object)oNews;
                        break;
return oLinq;

oDB は私の DataContext です。oLinq は、更新するデータを含む単なるオブジェクトです。更新する必要があるテーブルの種類を指定するフィールドが含まれています。switch case を使用して、そのテーブルを指定することを決定します。私は今、ほとんど同じことをする4つの異なるケースを持っています。

しかし、これを動的に実行できるようにしたいので、このコードをすべて 4 回書き直す必要がなく、将来的に新しいテーブルを追加できるようになります。

これは私がしようとしているものです:

List<string> alsTableNames = (from tables in oDB.Mapping.GetTables() select tables.TableName).ToList();
                foreach (string sTableName in alsTableNames)
                {
                    if (String.Compare(sTableName.Substring(6), (string)property.GetValue(oLinq, null)) == 0)
                    {
                        string sBasicType = sTableName.Replace("dbo.", "");
                        string sType = "RegisterService." + sBasicType;
                        Type tType = Type.GetType(sType);
                        oLinq = Convert.ChangeType(oLinq, tType);

これは、oLinq オブジェクトを必要なタイプに変換するために機能します。しかし、うまくいかないのは、データベースからデータを取り出して新しいデータに置き換えることです。私は基本的に動的な方法でこれを行う方法が必要です:

List<t_news> newsList = (from n in oDB.t_news where n.ID.Equals(oNews.ID) select n).ToList();
                        t_news oNe = newsList[0];

何かのようなもの:

List<//type of the table I want> list = (from t in //table where t.//firstproperty.Equals(oLinq.getType().getProperties().toList()[0]) select t.ToList();
                        object oNewObj = list[0];

何か案は?

4

1 に答える 1

0

Dynamic LINQの使用を検討しましたか?

これを使用すると、次のようなことができます。

// Use reflection to figure out what type of entity you need to update
var tYourEntityType = /* your code to get the type of the object you will be working with */;

// Get the entity you need to update querying based on a dynamic where clause
string propetyNameToSeach = "firstproperty"; // set this dynamically to the name of the property
string propertyValueToCompare = "5"; // set this dynamically to the value you want to compare to
var entityToUpdate = youDataContext.GetTable<tYourEntityType>().Where(propetyNameToSeach + " = " + propertyValueToCompare).FirstOrDefault();
于 2012-04-26T20:18:08.323 に答える