【问题标题】:from and select in c# .net?在 c# .net 中从和选择?
【发布时间】:2010-12-01 07:51:15
【问题描述】:

谁能告诉我如何在下面给出的选择语句中指定特定列:

var combinedrows = from dt1 in DsResults.Tables[0].AsEnumerable()
                   join dt2 in DsResults.Tables[1].AsEnumerable()
                     on dt1.Field<string>("MethodName") equals dt2.Field<string>("MethodName")
                   select new { dt1, dt2 };

DataTable finaldt = new DataTable("FinalTable");
finaldt.Columns.Add(new DataColumn("sp", typeof(string)));
finaldt.Columns.Add(new DataColumn("Method", typeof(string)));
finaldt.Columns.Add(new DataColumn("Class", typeof(string)));
finaldt.Columns.Add(new DataColumn("BLLMethod", typeof(string)));
DataRow newrow = finaldt.NewRow();           
foreach (var row in combinedrows)
{

    DataRow dataRow = finaldt.NewRow();
    dataRow.ItemArray = row.dt1.ItemArray;

     finaldt.Rows.Add(dataRow);
}

【问题讨论】:

    标签: c# .net join datatable


    【解决方案1】:

    如果要指定要选择的列,则应尝试更改

    combinedrows = from dt1 in DsResults.Tables[0].AsEnumerable() join
    dt2 in DsResults.Tables[1].AsEnumerable() on dt1.Field("MethodName") equals
    dt2.Field("MethodName") select new { dt1, dt2 };
    

    combinedrows = from dt1 in DsResults.Tables[0].AsEnumerable() join
    dt2 in DsResults.Tables[1].AsEnumerable() on dt1.Field("MethodName") equals
    dt2.Field("MethodName") select new 
    {
         dt1.columnName;
         dt2.columnName2;
         dt2.columnName3;
         etc.
    }
    

    希望这是您所寻找的。​​p>

    你可以看看LinQ-CheatSheet

    【讨论】:

      【解决方案2】:

      嗯,你想要什么并不是很清楚,但也许是这样的?

      var combinedRows = from dt1 in DsResults.Tables[0].AsEnumerable()
                         join dt2 in DsResults.Tables[1].AsEnumerable()
                           on dt1.Field<string>("MethodName") 
                           equals dt2.Field<string>("MethodName")
                         select new { MethodName = dt1.Field<string>("MethodName"),
                                      Foo = dt2.Field<string>("Foo"),
                                      Bar = dt1.Field<int>("Bar") };
      

      【讨论】:

        【解决方案3】:

        不知道你的问题是什么... linq 查询是 dsResults.Tables[0] 和 dsResults.Tables[1] 之间基于 MethodName 字段的常规连接。

        你想做什么?

        【讨论】:

        • 我尝试加入两个数据表 dt1 和 dt2。但结果集必须只包含两个数据表中的选定列。
        【解决方案4】:

        @Prem:使用此代码,它工作正常,检查一下。

        var combinedrows = from dt1 in DsResults.Tables[0].AsEnumerable()
                                      join dt2 in DsResults.Tables[1].AsEnumerable() on        dt1.Field<string>("MethodName") equals dt2.Field<string>("MethodName")
                                   select new { sp = dt1.Field<string>("Tab1_col1"), Method = dt1.Field<string>("MethodName"), _class = dt1.Field<string>("Class"),
                                                BLLMethod = dt1.Field<string>("Tab2_col2")
                                   }; 
        
        
                DataTable finaldt = new DataTable("FinalTable");
                finaldt.Columns.Add(new DataColumn("sp", typeof(string)));
                finaldt.Columns.Add(new DataColumn("Method", typeof(string)));
                finaldt.Columns.Add(new DataColumn("Class", typeof(string)));
                finaldt.Columns.Add(new DataColumn("BLLMethod", typeof(string)));
                DataRow newrow = finaldt.NewRow();           
                foreach (var row in combinedrows)
                {
                    DataRow dr = finaldt.NewRow();
                    dr["sp"] = row.sp;
                    dr["Method"] = row.Method;
                    dr["Class"] = row._class;
                    dr["BLLMethod"] = row.BLLMethod;
                    finaldt.Rows.Add(dr);
                }
        

        【讨论】:

          【解决方案5】:

          请阅读您的评论。我认为您想要的不是加入,而是以下内容:

          var combinedrows = 
              from dt1 in DsResults.Tables[0].AsEnumerable()
              from dt2 in DsResults.Tables[1].AsEnumerable()
              where dt1.Field<string>("MethodName") equals dt2.Field<string>("MethodName")
              select new { dt1, dt2 };
          

          它只返回两个表中具有相同“MethodName”的行。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-02-12
            • 2011-02-04
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多