C# 使用变量调用Linq to Sql中的表和列
我有一个问题,从逻辑上看似乎很简单,但从sql角度看却不简单 我有一个程序,允许用户选择一个单选按钮,checkChange事件将按钮的文本字符串发送到一个函数,该函数将其与数据库进行比较,该数据库返回列标题的名称和列作为keyValuePair来自的表的名称。到目前为止很简单 共有8个表,每个表有10到21个可能的选项,每个选项在其中一个表中是一个单独的列 我尝试使用字符串变量作为表名调用一个表,在keyValuePair.value中命名,然后从keyValuePair.key命名的列中选择所有数据。 如果这有道理的话 编辑 有一类静态方法初始化数据上下文并获取表,每个方法名称由get+TableName+Table组成,因此我们有例如getDescriptionsTable,这个类称为Initialise。getTableValues返回一个KeyValuePairC# 使用变量调用Linq to Sql中的表和列,c#,sql,sql-server,linq,C#,Sql,Sql Server,Linq,我有一个问题,从逻辑上看似乎很简单,但从sql角度看却不简单 我有一个程序,允许用户选择一个单选按钮,checkChange事件将按钮的文本字符串发送到一个函数,该函数将其与数据库进行比较,该数据库返回列标题的名称和列作为keyValuePair来自的表的名称。到目前为止很简单 共有8个表,每个表有10到21个可能的选项,每个选项在其中一个表中是一个单独的列 我尝试使用字符串变量作为表名调用一个表,在keyValuePair.value中命名,然后从keyValuePair.key命名的列中选择
KeyValuePair<string, string> names = new getTableValues();
string colHead = names.Key; // contains the column heading
string tabName = name.Value; // contains the Table name
string tableName = "get" + tabName + "Table()" // produce a getTable name
MyDataContext mdc = Initialise.tableName; // Initialise is a class for getting tables.
// table name is the concat variable above
var p = from x in mdc.tableName // here I want to use the variable tableName
where !x.colHead.equals( null) && // here I want to use the variable colHead
!x.colHead.contains("") // " " " " " " " "
select new { Code = x.code, Property = Convert.ToDecimal(x.colHead) };
var y = from output in p
orderby y.Code descending
select new { Code = y.Code, Property = y.Property};
DataGridView1.DataSource = y;
class Initialise
{
#region Full Table
public static System.Data.Linq.Table<Login> GetLoginDetails()
{
DataClasses2DataContext log = new DataClasses2DataContext();
return log.GetTable<Login>();
}
public static System.Data.Linq.Table<Descriptions> GetDescriptionsTable()
{
DataClasses1DataContext dc = new DataClasses1DataContext();
return dc.GetTable<Descriptions>();
}
public static System.Data.Linq.Table<Singles> GetSingleTable()
{
DataClasses1DataContext dc = new DataClasses1DataContext();
return dc.GetTable<Singles>();
}
public static System.Data.Linq.Table<Doubles> GetDoubleTable()
{
DataClasses1DataContext dc = new DataClasses1DataContext();
return dc.GetTable<Doubles>();
}
#endregion
}
此时,我还没有在第二个表上实现连接。等我对这个问题有了更充分的了解之后,我会有更多的机会的
我想SQL等价物应该是
从@tableName中选择@colName
其中@colName==NotNull
和@colName!=
订购人@colName desc
我是LinqtoSQL的新手,可能有一种更简单的方法。但这可能根本不可能。这将意味着很多if-else语句。我尝试了几种方法,我认为最简单的方法就是这样 1插件项目参考,可从NuGet获得 2.像这样更改代码
KeyValuePair<string, string> names = new getTableValues();
string colHead = names.Key; // contains the column heading
string tabName = name.Value; // contains the Table name
string tableName = "get" + tabName + "Table()" // produce a getTable name
var table = typeof(Initialise).GetMethod(tableName).Invoke(null, null); // Initialise is a class for getting tables.
var data = ((IQueryable)table).Where(string.Format("!{0}.Equals(@0) && !{0}.Equals(@1)", colHead), null, "")
.OrderBy("code")
.Select(string.Format("new(code as Code,Convert.ToDecimal({0}) as Property )",colHead));
DataGridView1.DataSource = data;
你好,我发现你想要什么有点难。上面的代码对你有用吗?在将项目添加到列表框后,是否需要帮助执行下一步操作?如上所述,我很难将其可视化。请发布其中一个目标表中的一些示例行以提供帮助。您在此处尝试检查的是tn.*colHead*.ToString>0?我想要的是使用变量(如select*from aVariable)调用一个表。或者最好还是从dbo.tableName d中选择d.colHead、p.name、p.code,在d.code==p.descCode上连接dbo.descriptions p,其中Convert.ToInt32d.colHead>0;ColHead和tableName都是变量