Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.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

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中的Select、group by和max查询_C#_Linq_Datatable - Fatal编程技术网

C# linq中的Select、group by和max查询

C# linq中的Select、group by和max查询,c#,linq,datatable,C#,Linq,Datatable,我不太了解linq,你能帮我解答这个问题吗 SELECT * FROM attachment WHERE create_date IN ( SELECT MAX (create_date) FROM attachment GROUP BY document_id, attachment_type) 如何将其更改为linq语句。我正在使用数据表 对不起,我

我不太了解linq,你能帮我解答这个问题吗

 SELECT   *
  FROM   attachment
 WHERE   create_date IN (  SELECT   MAX (create_date)
                             FROM   attachment
                         GROUP BY   document_id, attachment_type)
如何将其更改为linq语句。我正在使用数据表

对不起,我的数据表包含字段“附件id”、“文档id”、“附件类型”和“创建日期”。我真的不能使用dt.Select():(


我还需要把它改回DataTable或Datarow,虽然你的问题不是很清楚,但我假设这就是你想要的:

var orderedDocGroups = tbl.AsEnumerable()
.GroupBy(r => new
{
    DocID = r.Field<int>("document_id"),
    AttID = r.Field<int>("attachment_type"),
})
.Select(group => new{
    DocID = group.Key.DocID,
    AttID = group.Key.AttID,
    MaxCreationDateRow = group
        .OrderByDescending(r => r.Field<DateTime>("create_date"))
        .First()
}).OrderByDescending(x => x.MaxCreationDateRow.Field<DateTime>("create_date"));

foreach(var docGroup in orderedDocGroups)
{
    var docInfo = string.Join(", ", string.Format("document_id:{0} attachment_type:{1} create_date:{2}",
                    docGroup.DocID, docGroup.AttID, docGroup.MaxCreationDateRow.Field<DateTime>("create_date")));
    Console.WriteLine(docInfo);
}
下面是测试上述功能的示例代码:

var tbl = new DataTable();
tbl.Columns.Add("attachment_id",typeof(Int32));
tbl.Columns.Add("document_id",typeof(Int32));
tbl.Columns.Add("attachment_type",typeof(Int32));
tbl.Columns.Add("create_date",typeof(DateTime));

var rnd = new Random();
for(int i=0; i < 10; i++)
{
    tbl.Rows.Add(i, rnd.Next(1, 5), rnd.Next(1, 3), new DateTime(2012, 07, 24, rnd.Next(1, 24), 0, 0));
}
var tbl=new DataTable();
tbl.列。添加(“附件_id”,类型(Int32));
tbl.Columns.Add(“文件id”,类型(Int32));
tbl.列。添加(“附件类型”,类型(Int32));
tbl.Columns.Add(“创建日期”,typeof(DateTime));
var rnd=新随机数();
对于(int i=0;i<10;i++)
{
添加(i,rnd.Next(1,5),rnd.Next(1,3),新日期时间(2012,07,24,rnd.Next(1,24),0,0));
}

谢谢你的回答。我正试着用我的代码来理解它:)@Mr.Rendezvous:编辑了我的答案,因为我假设你想要每个组的最大创建日期行。很抱歉,我如何将其更改回datarow@Tim@Mr.Rendezvous:编辑我的答案以显示hwo,从而将其转换为仅包含MaxRows的DataTable。
var tbl = new DataTable();
tbl.Columns.Add("attachment_id",typeof(Int32));
tbl.Columns.Add("document_id",typeof(Int32));
tbl.Columns.Add("attachment_type",typeof(Int32));
tbl.Columns.Add("create_date",typeof(DateTime));

var rnd = new Random();
for(int i=0; i < 10; i++)
{
    tbl.Rows.Add(i, rnd.Next(1, 5), rnd.Next(1, 3), new DateTime(2012, 07, 24, rnd.Next(1, 24), 0, 0));
}