1

I love LINQ esp. LINQ to SQL. The use of LINQ for static tables (where the structure of the table is known before hand) is great.

I now have a list of tables where I only know the Table Name (as a string). I also know that each row in the table would have an ID/Primary Key(int) and a number of undetermined columns whose datatypes are varchar/string. I can get the names of the columns using SMO.

I am wondering what is the best way to query these columns e.g. run a distinct on each of them? LINQ to SQL seems to be pretty efficient by converting the query directly to SQL. I would prefer not to write the SQL by hand.

4

2 回答 2

0

LINQ 与类型一起工作——如果你没有为你的表生成类,你就不能真正使用 LINQ。您正在尝试在非强类型场景中使用强类型工具。

您可能需要一个 SQL 构建器库,例如http://www.codeproject.com/Articles/13419/SelectQueryBuilder-Building-complex-and-flexible-S

于 2012-05-21T16:15:26.057 回答
0

也许动态 linq 会满足您的需求

动态 LINQ(第 1 部分:使用 LINQ 动态查询库)

MSDN下载

片段:

// your columnnames would be more dynamic :)
string columnName1 = "id";
string columnName2 = "someOther";

// to execute a dynamic query simple put the columnnames with your 
yourContext.SomeItems
    .Where(string.Format("{0} == 123 and {1} > 4",columnName1, columnName2);
于 2012-05-21T16:18:48.403 回答