EF执行SQL返回动态类型

https://www.cnblogs.com/jacle169/p/4949654.html

 1 namespace ConsoleApplication2
 2 {
 3     class Program
 4     {
 5         static void Main(string[] args)
 6         {
 7             using (var entity = new ldhEntities())
 8             {
 9                 var context = ((IObjectContextAdapter)entity).ObjectContext;
10                 string myQuery = @"select affairinfo.ID, affairinfo.NAME from affairinfo";
11                 var data = (new ObjectQuery<DbDataRecord>(myQuery, context)).ToList();
12 
13                 foreach (DbDataRecord rec in data)
14                 {
15                     Console.WriteLine("ID {0}; NAME {1}", rec[0], rec[1]);
16 
17                    //Console.WriteLine("ID {0}; NAME {1}", rec["ID"], rec["NAME"]);
18                 }
19             }
20             Console.Read();
21         }
22     }
23 }

缺点是select语句中不能用”*“,并且字段必须带上表名

posted @ 2021-09-01 10:39  毛毛虫!  阅读(149)  评论(0编辑  收藏  举报