LINQ to SQL - CheckedListBox

时间:2015-09-02 13:19:01

标签: c# linq-to-sql

我有一个DataContext(Linq to Sql),大约有47个表。 我想获取所有47个表的列表并将它们打印到TabControl“Tables”下的CheckedListBox? 当用户在“Tables”TabControl下检查与CheckedListBox关联的任意数量的表名时 - 我希望在“Tables”TabControl上与CheckedListBox关联的所有列名都打印在 “列”TabControl。有人可以帮忙吗?感谢。

PropertyInfo[] properties = typeof(MyDataContext).GetProperties();
foreach (PropertyInfo property in properties)
{
    if(property.PropertyType.IsGenericType)
    { 
        object[] attribs = property.PropertyType.GetGenericArguments()[0].GetCustomAttributes(typeof(TableA‌​ttribute), false); 
        if(attribs.Length > 0)
        { 
            Console.WriteLine(property.Name); 
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我能想到的一个解决方案是使用Reflection来读取上下文类的所有属性。然后再次使用所选清单的属性读取属性来获取列名称。这是一个让你开始的指针

var tables = typeof(YourDataContextClassName).GetProperties(); // This will return you all public properties, use overloaded methods to get your desired properties

    // You have all tables now, get their name and then against each table, use the same technique to get their fields.