简体   繁体   English

linq to sql报表查询表

[英]linq to sql report tables in query

Here's the method i want to write: 这是我要写的方法:

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

where the IQueryable is a linq-to-sql query (is there a more specific interface i should use?). IQueryable是linq-to-sql查询(我应该使用更特定的接口吗?)。

then if i had a query like this 然后,如果我有这样的查询

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"]

basically it would show all the tables that this query touches in the db, it is ok to execute the query to figure this out too (since that is going to happen anyway)? 基本上,它将显示此查询在db中涉及的所有表,也可以执行查询以解决此问题(因为无论如何都会发生这种情况)? any ideas? 有任何想法吗?

(EDIT: sorry if this is a little too "give me teh codez!", any tips, partial solutions, or explanations of why this is impossible will be considered Thanks! -Luke) (编辑:对不起,如果这有点“给我codez!”,那么任何提示,部分解决方案或无法实现的解释都将被视为谢谢!-Luke)

LINQ-to-SQL allows you to capture the TSQL easily - just set .Log . LINQ-to-SQL允许您轻松捕获.Log只需设置.Log The problem then is parsing that. 然后问题就在解析。 I would be tempted to capture the TSQL, and play it back in SSMS with SET STATISTICS IO ON - this gives easily parsed per-table results. 很想捕获TSQL,并在SET STATISTICS IO ON在SSMS中播放它-这样可以轻松地解析每个表的结果。

您可以执行DataContext.GetCommand(query).CommandText ,然后解析查找FROM和JOIN子句(tricky是笛卡尔(逗号)联接)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM