积极答复者
如何将两个具有相同列的From子句形成的集合合并到一起?

问题
-
想要在保存表前,对表中各行某一列不重复的值进行相关操作,集合类似如下语句。如何能将q1、q2合并到一起,并去除二者重复的值呢?即利用一个From语句形成一个集合(类似于SQL Server中的 UNION ALL功能)
Dim q1 = From dr1 In table Where {DataRowState.Added, DataRowState.Modified}.Contains(dr1.RowState) Select dr1.某列 Distinct
Dim q2 = From dr2 In table Where dr2.RowState = DataRowState.Deleted Select dr2("某列", DataRowVersion.Original) Distinct
韩立学
- 已编辑 大海怪 2013年6月25日 3:51
答案
-
Module Module1 Sub Main() Dim dt As New DataTable Randomize() dt.Columns.Add("Id", GetType(Int32)) For index = 1 To 10 dt.Rows.Add(Rnd() * 5 + 1) Next dt.AcceptChanges() '打印 For Each row In dt.Rows Console.WriteLine(row(0)) Next '改变 dt.Rows(0)(0) = 1 dt.Rows(1)(0) = 2 '删除 dt.Rows(2).Delete() dt.Rows(3).Delete() Console.WriteLine("==========") Console.WriteLine("Changed:") '打印 For Each row As DataRow In dt.Rows If (row.RowState = DataRowState.Modified) Then Console.WriteLine("From " & row(0, DataRowVersion.Original) & " To " & row(0, DataRowVersion.Current)) End If Next Console.WriteLine("Deleted:") '打印 For Each row As DataRow In dt.Rows If (row.RowState = DataRowState.Deleted) Then Console.WriteLine("Deleted Number:" & row(0, DataRowVersion.Original)) End If Next '查询 Dim result = ((From row In dt.AsEnumerable() Where row.RowState = DataRowState.Modified Select row(0)).Union(From r In dt.AsEnumerable() Where r.RowState = DataRowState.Deleted Select r(0, DataRowVersion.Original))).Distinct() Console.WriteLine("======") '打印 For Each row In result Console.WriteLine(row) Next End Sub End Module
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
Found any spamming-senders? Please report at: Spam Report- 已标记为答案 大海怪 2013年6月25日 6:33
全部回复
-
請參考一下以下的link,
http://stackoverflow.com/questions/9633121/datatable-union
'建立空的DataTable Dim dtResult = New DataTable("SC_1Funs") dtResult.Columns.Add("c1", Type.GetType("System.String")) dtResult.Columns.Add("c2", Type.GetType("System.String")) '新增資料到DataTable之中 dtResult.Rows.Add(New String() {"c1-1", "c2-1"}) dtResult.Rows.Add(New String() {"c1-2", "c2-2"}) Dim dtResult2 = dtResult.Clone() dtResult.Rows.Add(New String() {"c1-3", "c2-3"}) dtResult.Rows.Add(New String() {"c1-1", "c2-1"}) Dim comparer = New CustomComparer() Dim dtUnion = dtResult.AsEnumerable.Union(dtResult2.AsEnumerable(), comparer).CopyToDataTable() 'other comparer class Public Class CustomComparer Implements IEqualityComparer(Of DataRow) Public Function GetHashCode(ByVal obj As DataRow) As Integer Implements IEqualityComparer(Of DataRow).GetHashCode Return DirectCast(obj("c1"), String).GetHashCode() End Function Public Function Equals(ByVal x As DataRow, ByVal y As DataRow) As Boolean Implements IEqualityComparer(Of DataRow).Equals Return DirectCast(x("c1"), String).Equals(DirectCast(y("c1"), String)) End Function End Class
亂馬客blog: http://www.dotblogs.com.tw/rainmaker/
- 已编辑 亂馬客 2013年6月25日 5:16 補上Code
-
Module Module1 Sub Main() Dim dt As New DataTable Randomize() dt.Columns.Add("Id", GetType(Int32)) For index = 1 To 10 dt.Rows.Add(Rnd() * 5 + 1) Next dt.AcceptChanges() '打印 For Each row In dt.Rows Console.WriteLine(row(0)) Next '改变 dt.Rows(0)(0) = 1 dt.Rows(1)(0) = 2 '删除 dt.Rows(2).Delete() dt.Rows(3).Delete() Console.WriteLine("==========") Console.WriteLine("Changed:") '打印 For Each row As DataRow In dt.Rows If (row.RowState = DataRowState.Modified) Then Console.WriteLine("From " & row(0, DataRowVersion.Original) & " To " & row(0, DataRowVersion.Current)) End If Next Console.WriteLine("Deleted:") '打印 For Each row As DataRow In dt.Rows If (row.RowState = DataRowState.Deleted) Then Console.WriteLine("Deleted Number:" & row(0, DataRowVersion.Original)) End If Next '查询 Dim result = ((From row In dt.AsEnumerable() Where row.RowState = DataRowState.Modified Select row(0)).Union(From r In dt.AsEnumerable() Where r.RowState = DataRowState.Deleted Select r(0, DataRowVersion.Original))).Distinct() Console.WriteLine("======") '打印 For Each row In result Console.WriteLine(row) Next End Sub End Module
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
Found any spamming-senders? Please report at: Spam Report- 已标记为答案 大海怪 2013年6月25日 6:33