none
Can't get data by Dynamic Mapping Source. RRS feed

  • Question

  • I want to select data from database with a dynamic table name. And I find some codes on the Internet, then I call getRainTable()

    method to get the data. Then I got a list of empty Entity. The count of the list is same with datas in database.But they are all

    empty.Is there anyone expert in LINQ can help me???So Anxious :(

    class DynamicMappingSource : MappingSource { class DynamicAttributedMetaModel : MetaModel { private MetaModel source; private const string TypeName = "System.Data.Linq.Mapping.AttributedMetaModel"; private DynamicMappingSource mappingSource; internal DynamicAttributedMetaModel(MappingSource mappingSource, Type contextType) { this.mappingSource = (DynamicMappingSource)mappingSource; var bf = BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.CreateInstance; var args = new object[] { mappingSource, contextType }; source = typeof(DataContext).Assembly.CreateInstance(TypeName, false, bf, null, args, CultureInfo.CurrentCulture, null) as MetaModel; Debug.Assert(source != null); } public override MetaTable GetTable(Type rowType) { if (mappingSource.GetMetaTableName != null) { var typeName = "System.Data.Linq.Mapping.AttributedMetaTable"; var bf = BindingFlags.GetField | BindingFlags.Instance | BindingFlags.NonPublic; var attribute = new TableAttribute { Name = mappingSource.GetMetaTableName(rowType) }; var args = new object[] { source, attribute, rowType }; var metaTable = typeof(DataContext).Assembly.CreateInstance(typeName, false, bf, null, args, CultureInfo.CurrentCulture, null) as MetaTable; return metaTable; } return source.GetTable(rowType); } public override MetaFunction GetFunction(MethodInfo method) { return source.GetFunction(method); } public override IEnumerable<MetaTable> GetTables() { return source.GetTables(); } public override IEnumerable<MetaFunction> GetFunctions() { return source.GetFunctions(); } public override MetaType GetMetaType(Type type) { return source.GetMetaType(type); } public override MappingSource MappingSource { get { return source.MappingSource; } } public override Type ContextType { get { return source.ContextType; } } public override string DatabaseName { get { return source.DatabaseName; } } public override Type ProviderType { get { return source.ProviderType; } } } public Func<Type, string> GetMetaTableName; protected override MetaModel CreateModel(Type dataContextType) { if (dataContextType == null) { throw new ArgumentNullException("dataContextType"); } return new DynamicAttributedMetaModel(this, dataContextType); } } public static List<DbRain> getRainTable(DateTime time) { var mappingSource = new DynamicMappingSource(); string appendix = ""; mappingSource.GetMetaTableName = delegate (Type type) { return appendix; }; var constr = @"Data Source=;Initial Catalog=;user id=;password="; var context = new DataContext(constr, mappingSource) { Log = Console.Out }; appendix = "rain" + time.Year + time.Month); var list = context.GetTable<DbRain>().Select(o => o).ToList(); return list; }


    Thursday, November 2, 2017 1:58 PM

Answers

All replies

  • I'm sorry for putting the description in the code field...didn't notice that..
    Thursday, November 2, 2017 1:59 PM
  • Hi  小海星一只,

    Following threads should help you.

    https://stackoverflow.com/questions/25859143/linq-select-from-dynamic-tablename-string

    https://stackoverflow.com/questions/28099435/dynamic-table-name-in-linq

    Sincerely,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, November 3, 2017 10:04 AM
  • Thank you vary much!I didn't even put any hope on this issue.But you solve my problem perfectly! And I use Reflection to get the properties value of each element in the Object Collection like this:

    foreach (Object o in records)
                    {
                        var time = (DateTime)o.GetType().GetProperty("datatime").GetValue(o);
                        var stationid = (string)o.GetType().GetProperty("stationid").GetValue(o);
                        if (time <= toTime && time >= fromTime && stationid == stationId.Substring(4))
                        {
                            result.Add(new RainDbData() { datatime = time, stationid = stationid});
                        }
                    }

    Thanks for your help again!

    Saturday, November 4, 2017 3:01 AM