100以上のテーブルを持つDataContext(Linq to Sql)があります。これらのテーブルのリストを取得してコンソールに出力することは可能ですか?これは愚かな質問かもしれません。Linq:DataContext内のすべてのテーブルのリストを取得
ありがとうございました。
100以上のテーブルを持つDataContext(Linq to Sql)があります。これらのテーブルのリストを取得してコンソールに出力することは可能ですか?これは愚かな質問かもしれません。Linq:DataContext内のすべてのテーブルのリストを取得
ありがとうございました。
これは、上記よりはるかに簡単であり、リフレクションは必要ありません。 Linq to SQLには、すべてのテーブルの列挙を取得するために使用できるマッピングプロパティがあります。
context.Mapping.GetTables();
これを反映させることができます。基本的には、DataContextクラスのプロパティを反復処理します。各プロパティについて、そのプロパティの汎用パラメータタイプにTableAttribute属性があるかどうかを確認します。その場合、そのプロパティは表を表します。
using System.Reflection;
using System.Data.Linq.Mappings;
PropertyInfo[] properties = typeof(MyDataContext).GetProperties();
foreach (PropertyInfo property in properties)
{
if(property.PropertyType.IsGenericType)
{
object[] attribs = property.PropertyType.GetGenericArguments()[0].GetCustomAttributes(typeof(TableAttribute), false);
if(attribs.Length > 0)
{
Console.WriteLine(property.Name);
}
}
}
dc= new myDataContext();
var listaTablas = (from tables in dc.Mapping.GetTables() select tables.TableName).ToList();
using System.Reflection;
using System.Data.Linq.Mappings;
PropertyInfo[] properties = typeof(MyDataContext).GetProperties();
foreach (PropertyInfo property in properties)
{
if(property.PropertyType.IsGenericType)
{
object[] attribs = property.PropertyType.GetGenericArguments()[0].GetCustomAttributes(typeof(TableAttribute), false);
if(attribs.Length > 0)
{
Console.WriteLine(property.Name);
}
}
}
ニース、私はそれについて知らなかった。 –
whooo hooそれはさらに優れています! – Sergey
ええ、私は自分のようなものを探し続けています。 :)を渡すのはいつも楽しいです。 –