Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用linq和Join以及使用DataTables的Max_C#_Linq_Join_Datatable_Max - Fatal编程技术网

C# 使用linq和Join以及使用DataTables的Max

C# 使用linq和Join以及使用DataTables的Max,c#,linq,join,datatable,max,C#,Linq,Join,Datatable,Max,请原谅我的新手尝试,但我已经在这个愚蠢的问题上花了一周的时间,并决心使用LINQ 这是我的SQL查询-使用SQL查询生成器生成 SELECT TABLE1.ID, MAX(DISTINCT TABLE2.TEXT) AS Expr1 FROM TABLE1 INNER JOIN TABLE2 ON TABLE1.ID = TABLE2.PARENT_ID GROUP BY TABLE1.ID 我想显示Table1行和table2最后一行 ID文本 1“记

请原谅我的新手尝试,但我已经在这个愚蠢的问题上花了一周的时间,并决心使用LINQ

这是我的SQL查询-使用SQL查询生成器生成

    SELECT TABLE1.ID, MAX(DISTINCT TABLE2.TEXT) AS Expr1

    FROM TABLE1 

    INNER JOIN TABLE2 ON TABLE1.ID = TABLE2.PARENT_ID

    GROUP BY TABLE1.ID
我想显示Table1行和table2最后一行 ID文本 1“记录1的最终评论” 2“记录2的最终评论”

使用C我有两个数据表

DataTable DT_Nodes = sess_nodes.ds.Tables["TABLE1"];
DataTable DT_Sticky = sess_nodes.ds.Tables["TABLE2"];

var linq_test = from tab1 in DT_Nodes.AsEnumerable()
join tab2 in DT_Sticky.AsEnumerable()
on tab1["ID"] equals tab2["PARENT_ID"]
group tab1 by tab1.Field<long>("ID") into result
select <I am stuck here>;
我想使用循环显示结果

foreach(DataRow resultrow in linq_test)
{
<stuck here also>
long id = resultrow.table1["ID"];     // This needs to be the ID in table1
long id_tab2 = resultrow.table2["ID"] // This needs to be the last if the ID's of table2
}

我尝试过各种方法,但是linq语法打败了我,而且web上的大多数示例都没有使用DataTables。

首先,请注意,上帝给了我们linq,所以我们可以停止使用DataTables

DataTable DT_Nodes = sess_nodes.ds.Tables["TABLE1"];
DataTable DT_Sticky = sess_nodes.ds.Tables["TABLE2"];

var linq_test = from tab1 in DT_Nodes.AsEnumerable()
    join tab2 in DT_Sticky.AsEnumerable()
            on tab1["ID"] equals tab2["PARENT_ID"]
    group new {Table1=tab1, Text=tab2["TEXT"]}
     by tab1.Field<long>("ID") into result
    select new {
             Id = result.Key,
             Text = result.Last().Text
             };
如果表1中唯一需要的是ID,那么我们可以将其简化一点:

var linq_test = from tab1 in DT_Nodes.AsEnumerable()
    join tab2 in DT_Sticky.AsEnumerable()
            on tab1["ID"] equals tab2["PARENT_ID"]
    group tab2["TEXT"] by tab1.Field<long>("ID") into result
    select new {
                 Id = result.Key,
                 Text = result.Last()
               }

詹姆斯,非常感谢。Text=result.Last.selectl=>l.Text行不编译。当我删除.selectl=l.Text时,它会删除,但不会返回任何结果。