【问题标题】:Using Variables to call a table and columns in Linq to Sql在 Linq to Sql 中使用变量调用表和列
【发布时间】:2014-03-06 16:29:19
【问题描述】:

我有一个问题,在逻辑上看起来很简单,但在 sql 上却不是

我有一个程序允许用户选择单个单选按钮,checkChange 事件将按钮的文本字符串发送到一个函数,该函数将它与返回列标题名称和名称的数据库进行比较列来自作为 keyValuePair 的表。到目前为止很简单。

有 8 个表,每个表有 10 到 21 个可能的选择,每个选择是其中一个表中的单独列。

我要做的是使用字符串变量作为表名调用表,在 keyValuePair.value 中命名,然后从 keyValuePair.key 命名的列中选择所有数据。 如果这有意义的话。

已编辑。

有一类静态方法初始化数据上下文并获取表,每个方法名称由“get”+TableName+“Table()”组成,所以我们有例如getDescriptionsTable(),这个类是称为初始化。 getTableValues() 返回一个 KeyValuePair。

    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 排序

我是 Linq to SQL 的新手,可能有更简单的方法可以做到这一点。但这可能根本不可能。这将意味着很多 if else 语句。

【问题讨论】:

  • 您好,找到您想要的东西有点困难。上面的代码对你有用吗?将项目添加到列表框后,您需要下一步的帮助吗?
  • 如上所述,我很难想象这一点。请从其中一个目标表中发布一些示例行以提供帮助。
  • 你试试看这里tn.*colHead*.ToString() &gt; 0?
  • 我想要的是使用诸如 select * from aVariable 之类的变量来调用表。
  • 或者最好还是选择 d.colHead, p.name, p.code from dbo.tableName d, join dbo.descriptions p on d.code == p.descCode where Convert.ToInt32(d. colHead) > 0; ColHead 和 tableName 都是变量

标签: c# sql sql-server linq


【解决方案1】:

我尝试了几种方法,我认为最简单的方法就是这样

1) 添加对System.Linq.Dynamic 的项目引用,可从 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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-15
    • 1970-01-01
    • 1970-01-01
    • 2011-10-03
    • 1970-01-01
    相关资源
    最近更新 更多