Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Comparing DataSets using LINQ

Status
Not open for further replies.

SBendBuckeye

Programmer
May 22, 2002
2,166
0
0
US
Can anyone help me translate this C# Linq code into VB.Net? It came from Comparing DataSets using LINQ: Thanks!

var orig = dsOriginal.Tables[0].AsEnumerable();
var updated = dsChanged.Tables[0].AsEnumerable();

//First, getting new records if any
var newRec = from u in dsChanged
where !(from o in orig
select o.Field<decimal>("PRIMARY_KEY_FIELD"))
.Contains(u.Field<decimal>(" PRIMARY_KEY_FIELD"))
select new
{
prim_key = u.Field<decimal>("PRIMARY_KEY_FIELD"),
field1 = u.Field<decimal>("FIELD1"),
field2=u.Field<decimal>("FIELD2"),
field3 = u.Field<decimal>("FIELD3"),
field4 = u.Field<decimal>("FIELD4"),
rec_type="A"//Added
};

//Secondly, getting updated records
var updRec = from u in updated
join o in orig
on u.Field<decimal>("PRIMARY_KEY_FIELD")
equals o.Field<decimal>("PRIMARY_KEY_FIELD")
where (u.Field<decimal>("FIELD1") !=
o.Field<decimal>("FIELD1")) ||
(u.Field<decimal>("FIELD2") !=
o.Field<decimal>("FIELD2"))
select new
{
prim_key = u.Field<decimal>("PRIMARY_KEY_FIELD"),
field1 = u.Field<decimal>("FIELD1"),
field2=u.Field<decimal>("FIELD2"),
field3 = u.Field<decimal>("FIELD3"),
field4 = u.Field<decimal>("FIELD4"),
rec_type = "M"//Mofified
};

var Union = newRec.Union(updRec);
 
here are language translators on the net. that can do this for you.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Hello Jason,

None of the language translators I normally use will handle this. Do you have a specific link I could try?
 
Here is the code. In the Project, Compile tab, Option Infer must be set to On.

Code:
Dim orig = dsOriginal.Tables(0).AsEnumerable()
Dim updated = dsChanged.Tables(0).AsEnumerable()

' First, getting new records if any.
Dim newRec = From u In updated _
    Where Not (From o In orig _
        Select o.Field(Of Decimal)("PRIMARY_KEY_FIELD")).Contains(u.Field(Of Decimal)("PRIMARY_KEY_FIELD")) _
    Select New With { _
        .prim_key = u.Field(Of Decimal)("PRIMARY_KEY_FIELD"), _
        .field1 = u.Field(Of Decimal)("FIELD1"), _
        .field2 = u.Field(Of Decimal)("FIELD2"), _
        .field3 = u.Field(Of Decimal)("FIELD3"), _
        .field4 = u.Field(Of Decimal)("FIELD4"), _
        .rec_type = "A" _
    } 'Added

' Secondly, getting updated records.
Dim updRec = From u In updated _
    Join o In orig On u.Field(Of Decimal)("PRIMARY_KEY_FIELD") Equals o.Field(Of Decimal)("PRIMARY_KEY_FIELD") _
        Where (u.Field(Of Decimal)("FIELD1") <> o.Field(Of Decimal)("FIELD1")) OrElse (u.Field(Of Decimal)("FIELD2") <> o.Field(Of Decimal)("FIELD2")) _
    Select New With { _
        .prim_key = u.Field(Of Decimal)("PRIMARY_KEY_FIELD"), _
        .field1 = u.Field(Of Decimal)("FIELD1"), _
        .field2 = u.Field(Of Decimal)("FIELD2"), _
        .field3 = u.Field(Of Decimal)("FIELD3"), _
        .field4 = u.Field(Of Decimal)("FIELD4"), _
        .rec_type = "M" _
    } 'Modified

Dim Union = newRec.Union(updRec)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top