1

I try to run LINQ query. As you can see from the code below, I try to name the table ("TableName") before passing it to LINQ, and of course LINQ can't find a table named "TableName". Any other way around how to do this? I tried ctxdata.(TableName) but didn't work.

            var TableName = "";
            TableName = "DATA_ + DistrictCode;

            using (SFTEMPEntities1 ctxdata = new SSFTEMPEntities1())
            {
                var queryAttributes = (from p in ctxdata.TableName //See this line 
                                   where p.Filename == FileName
                                   select new { p.REF , p.JOB_TITLE })
                                   .Take(1);


            }

I'm still a newbie on C# , really appreciate if you can show me example based on this code.

Waller
  • 433
  • 1
  • 6
  • 20
  • Maybe this question (and the linked questions) can help http://stackoverflow.com/questions/28099435/dynamic-table-name-in-linq – robor Oct 06 '16 at 07:48

1 Answers1

1

You can get table from data context using Set method but you need type of it's elements like generic parameter Set<T>() or just Type variable Set(Type type). So you need to find some way to get type by table name. If you have type name you can get type by name from assembly ctxdata.GetType().Assembly.GetType("TypeName").

Using reflection you can get table property from data context like this:

var table = ctxdata.GetType().GetProperty(TableName).GetValue(ctxdata) as IQueryable;
Andrey Tretyak
  • 3,043
  • 2
  • 20
  • 33