代码之家  ›  专栏  ›  技术社区  ›  Alexander Prokofyev

是否可以从DataContext.ExecuteQuery返回匿名对象的IEnumerable?

  •  3
  • Alexander Prokofyev  · 技术社区  · 15 年前

    我开发了一个报告引擎,其中报告基于模板。每个模板都有带有SQL查询的字符串,每个报表都有特定的SQL查询参数值。要呈现报表,我设置参数并调用 DataContext.ExecuteQuery 获取记录列表的方法。但要捕获返回的列,我必须知道它们的名称,并有一个具有相应属性的类。

    是否可以从DataContext.ExecuteQuery返回匿名对象的IEnumerable,然后使用反射确定其属性?

    我需要一个相当于LINQ的 SqlDataReader.GetValues .

    谢谢!

    3 回复  |  直到 13 年前
        1
  •  4
  •   Alexander Prokofyev    15 年前

    直到我们有了C 4.0 迪纳利 关键字我们可以使用这个解决方案(稍微修改了一篇文章中的代码 Executing arbitrary queries in LINQ to SQL 作者:奥克塔维奥·赫恩·恩德斯·莱尔:

    public static class DataContextExtension
    {
        public static IEnumerable<Dictionary<string, object>> ExecuteQuery(this DataContext dataContext, string query)
        {
            using (DbCommand command = dataContext.Connection.CreateCommand())
            {
                command.CommandText = query;
                dataContext.Connection.Open();
    
                using (DbDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    while (reader.Read())
                    {
                        Dictionary<string, object> dictionary = new Dictionary<string, object>();
    
                        for (int i = 0; i < reader.FieldCount; i++)
                            dictionary.Add(reader.GetName(i), reader.GetValue(i));
    
                        yield return dictionary;
                    }
                }
            }
        }
    }
    

    此扩展方法返回字典<>对象的IEnumerable,其中键是查询列的名称。

        2
  •  0
  •   Valentin V    15 年前

    是的,你能做到。 请看一下这个片段。

    class Program {
            static void Main(string[] args) {
                var persons = new Person[]{
                    new Person{Age=22,Name="John Doe",Id=1},
                    new Person{Age=23,Name="Jack Smith", Id=2},
                    new Person{Age=34,Name="Sara Parker", Id=3}
                };
                var anonData = GetAnonTypes(persons);
                foreach (var item in anonData as IEnumerable) {
                    //use reflection to access propties 
                }
            }
            static object GetAnonTypes(IEnumerable<Person> persons) {
                var query=from p in persons  select new{
                    Id=p.Id,
                    Name=p.Name
                };
                return query;        
            }
        }
    
        public class Person {
            public int Id { get; set; }
            public string Name { get; set; }
            public int Age { get; set; }
        }
    
        3
  •  -4
  •   Amy B    13 年前
    • 编译器无法打开SQL并确定应该存在的属性。
    • 既然您希望编译器做到这一点,我必须得出结论,即您并不真正理解匿名输入。

    不要为此使用LinqToSQL。只需使用数据阅读器方法。