[英]Linq first value from grouping - Why is this not working?
I asked a question here about how to get specific data from a datatable using Linq and have come up with a solution I thought could do the trick, but it's not giving me what I expected. 我在这里问了一个问题,该问题是如何使用Linq从数据表中获取特定数据,并提出了我认为可以解决问题的解决方案,但并没有给我期望的结果。
Basically, suppose I have the following data to query: 基本上,假设我要查询以下数据:
Col_A: Col_B: Col_C: Col_D: Date1: Date2:
a1 b1 c1 d1 Dt1_1 Dt2_1
a1 b2 c2 d2 Dt1_1 Dt2_2
a1 b3 c3 d3 Dt1_1 Dt2_3
a1 b4 c4 d4 Dt1_2 Dt2_4
a1 b5 c5 d5 Dt1_2 Dt2_5
And, in terms of dates, assume, for example, "_1" < "_2". 并且,就日期而言,假设例如“ _1” <“ _ 2”。 So, what I want is, for each value in
Col_A
, to get one row for each Date1
for the earliest Date2
. 因此,我想要的是,对于
Col_A
每个值,为最早的Date2
每个Date1
获取一行。
So, given this data, I'd want to get back: 因此,鉴于此数据,我想回来:
Col_A: Col_B: Col_C: Col_D: Date1: Date2:
a1 b1 c1 d1 Dt1_1 Dt2_1
a1 b4 c4 d4 Dt1_2 Dt2_4
In other words, since Date1
changed, we get the data for the earliest Date2
. 换句话说,由于
Date1
发生了更改,因此我们获得了最早的Date2
的数据。 I hope that makes sense. 我希望这是有道理的。
And I could not use a complex query to get the data due to it being from a very old DB model that doesn't support many modern grouping functions. 而且我无法使用复杂的查询来获取数据,因为它来自非常旧的数据库模型,该模型不支持许多现代分组功能。
So, what I came up with was: 因此,我想到的是:
SQL Query : SQL查询 :
SELECT Col_A, Col_B, Col_C, Col_D, Date1, Date2
FROM MyTable
ORDER BY Col_A, Date1, Date2
This gives me the ordered data and I query that into a DataTable in .Net. 这给了我有序的数据,然后我将其查询到.Net中的DataTable中。
Now, I basically want the first record for each value of Col_A
and Date1
combination, so I came up with this Linq statement: 现在,我基本上想要
Col_A
和Date1
组合的每个值的第一条记录,因此我Date1
了以下Linq语句:
Dim y As DataTable = (From dr As DataRow In OriginalData
Group dr By k = New With {.Col_A= dr.Field(Of String)("Col_A"), .Date1 = dr.Field(Of Date)("Date1")} Into grp = Group
Select grp.First).CopyToDataTable
And I thought that would do it, but it returns y
having the same number of rows as my original DataTable (in other words, it didn't remove all the duplicates). 而且我认为这样做可以,但是它返回的
y
与我的原始DataTable的行数相同(换句话说,它没有删除所有重复项)。
As I inspected it closer, it seems that it doesn't do the grouping - It basically sees each row as a new group...* 当我仔细检查时,似乎没有进行分组-它基本上将每一行视为一个新的分组... *
What am I doing wrong? 我究竟做错了什么? How can I change this Lync query to give me the correct results?
如何更改此Lync查询以为我提供正确的结果?
Also, even though the Linq is written in VB, I'm equally comfortable with C#. 而且,即使Linq是用VB编写的,我对C#也同样满意。
Thanks!!! 谢谢!!!
Hope this helps: 希望这可以帮助:
public class Test
{
public DateTime Date1;
public DateTime Date2;
public string Col_A;
}
... ...
List<Test> test = new List<Test>();
test.GroupBy(t => t.Col_A).Select(group =>
group.OrderBy(e => e.Date2).First())
ToList();
This should do what you want - first group by Col_A
and Date1
, then sort the groups by Date2
and pick the first item from each group. 通过第一组-这应该做你想要什么
Col_A
和Date1
,然后排序组Date2
,并从每一组挑选的第一个项目。
originalData.GroupBy(row => new { row.Col_A, row.Date1 })
.Select(group => group.OrderBy(groupRow => groupRow.Date2)
.First())
.ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.