如何将字符串转换为linq查询? [英] How do I convert a string to linq query?

查看:79
本文介绍了如何将字符串转换为linq查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,其中放置了linq查询的代码。如何将此字符串转换为linq查询?



例如:

I have a string in which the code for linq query is placed. How can I convert this string to linq query?

For ex:

string linqstring="(from t1 in Context.Table1
              join t2temp in Context.Table2 on t1.Id equals t2.Id into tempJoin
              from t2 in tempJoin.DefaultIfEmpty()
              select ...)";





如何将此字符串转换为linq查询?有预制功能吗?





我在这里粘贴我的实际实现。请看看打击







How can I convert this string to linq query? Is there any pre-built function?


I am pasting my actual implementation here.Please see blow


private void tableJoin(DataSet ds)
       {

           DataTable table;
           string linque = "from ";
           string condition = " on ";
           string end = "into g";
           DataGridView filteredGrid = new DataGridView();
           // string on = "from";
           if (ds.Tables.Count == 2)
           {
               for (int j = 0; j < ds.Tables.Count; j++) //now it is fo 2 tables
               {
                   int k = j + 1;
                   if (k >= ds.Tables.Count)
                   {
                       break;
                   }
                   DataTable table1 = ds.Tables[j];
                   DataTable table2 = ds.Tables[k];



                   if (ds.Tables.Count == 2)
                   {
                       dtab = (from a in table1.AsEnumerable()
                               join b in table2.AsEnumerable()
                               on a[cnames.ElementAt(j)] equals
                               b[cnames.ElementAt(k)] into g
                               from b in g.DefaultIfEmpty()
                               select a).CopyToDataTable();
                       j++;
                   }


               }
                  filteredGrid.DataSource = dtab;

           }
           else
           {
               int x = 0;

               for (int i = x; i < ds.Tables.Count; i++)
               {
                   table = new DataTable("table" + i.ToString());

                   linque += "t" + i + " in " + table + ".AsEnumerable() join ";
                   if (i >= 1)
                   {
                       condition += "t" + (i - 1).ToString() + "[" + cnames.ElementAt(i - 1) + "] equals t" + (i).ToString() + "[" + cnames.ElementAt(i) + "] and ";
                   }
                   x = i;
               }
               int position1 = linque.LastIndexOf("join");
               string lin = linque.Substring(0, position1);

               int position2 = condition.LastIndexOf("and");
               string condi = condition.Substring(0, position2);
               //MessageBox.Show("Linque:" + lin + condi + end + " from t[" + x + "] in g.DefaultIfEmpty() select t0");
               var finale = lin + condi + end + " from t[" + x + "] in g.DefaultIfEmpty() select t0";

               filteredGrid.DataSource = finale; //getting error here

           }



           filteredGrid.Top = panelColumns.Bottom + 200;
           filteredGrid.ForeColor = Color.BurlyWood;
           filteredGrid.Width = 300;
           filteredGrid.Height = 200;
           filteredGrid.Left = panelColumns.Left;
           panelCompare.Controls.Add(filteredGrid);


       }





这里,我有一个linq查询代码字符串名为结局。如何将此字符串转换为实际的linq查询并执行它以获得数据表中的结果?



Here,I have the linq query code in a string named "finale". How can I convert this string to actual linq query and execute it to get result in a datatable?

推荐答案

虽然我之前没有这样做,但希望以下链接将帮助

如何转换一个字符串到它的等效表达式树? [ ^ ]
While I haven't done this earlier but hopefully following link will help
How to convert a String to its equivalent Expression Tree?[^]


这篇关于如何将字符串转换为linq查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆