from and select in c# .net?

前端 未结 5 1000
后悔当初
后悔当初 2021-01-29 10:02

Can anyone Please tell me how to specify the particular column in the select statement given below:

var combinedrows = from dt1 in DsResults.Tables[0].AsEnumerab         


        
相关标签:
5条回答
  • 2021-01-29 10:51

    Just read your comment. I think what you want is not a join but the following:

    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 };
    

    It returns ony rows from both tables that have the same "MethodName".

    0 讨论(0)
  • 2021-01-29 10:55

    If you want to specify which columns you want to select, you should try changing

    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 };
    

    in

    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.
    }
    

    Hope this what you were looking for.

    You can have a look at the LinQ-CheatSheet

    0 讨论(0)
  • 2021-01-29 10:57

    @Prem: Use this code its working fine, check it out.

    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);
            }
    
    0 讨论(0)
  • 2021-01-29 11:01

    Not sure what your question is... The linq query is a regular join between dsResults.Tables[0] and dsResults.Tables[1] based on the MethodName field.

    What are you trying to do?

    0 讨论(0)
  • 2021-01-29 11:02

    Well, it's not really clear what you want, but something like this, perhaps?

    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") };
    
    0 讨论(0)
提交回复
热议问题