代码之家  ›  专栏  ›  技术社区  ›  luke

查询中的Linq to SQL报表表

  •  2
  • luke  · 技术社区  · 15 年前

    我想写的方法是:

    public static IEnumerable<String> GetTableNames(this IQueryable<T> query)
    {
        //...
    }
    

    iqueryable是linq-to-sql查询的地方(我是否应该使用更具体的接口?).

    如果我有这样的问题

    var q = from c in db.Customers
            from p in db.Products
            from cp in db.CustomerProducts
            where c.ID = 3 && cp.CustID == c.ID && p.ID == cp.ProdID
            select new {p.Name, p.Version};
    q.GetTableNames();// return ["Customers", "Products", "CustomerProducts"]
    

    基本上,它将显示这个查询在数据库中涉及的所有表,执行查询也可以解决这个问题(因为无论如何都会发生这种情况)?有什么想法吗?

    (编辑:对不起,如果这有点太“给我一个法码!”,任何提示,部分解决方案,或解释为什么这是不可能的将被视为感谢!-卢克)

    2 回复  |  直到 15 年前
        1
  •  1
  •   Marc Gravell    15 年前

    Linq to SQL允许您轻松捕获TSQL-只需设置 .Log . 接下来的问题就是分析这个问题。我会 诱惑 捕捉TSQL并在SSMS中播放 SET STATISTICS IO ON -这使得每个表的结果很容易解析。

        2
  •  0
  •   Amy B    15 年前

    你可以做到 DataContext.GetCommand(query).CommandText ,然后分析查找FROM和JOIN子句的内容(棘手的是笛卡尔(逗号)联接)。