Jared.Rodgers
Jared.Rodgers

Reputation: 141

Converting a VB code with an Aggregate clause to C#

I've been trying to convert the following the code from VB to C#:

Dim rowsnotfound As DataRow() = (From rowstb2 As DataRow In dsNew.Tables("parts").Rows.OfType(Of DataRow)() Where (Aggregate rowstb1 As DataRow In dsOld.Tables("parts").Rows.OfType(Of DataRow)() Where ((rowstb1.Item("TRANSACTION")) = (rowstb2.Item("TRANSACTION")) And (rowstb1.Item("DESCRIPTION")) = (rowstb2.Item("DESCRIPTION")) And (rowstb1.Item("QTY")) = (rowstb2.Item("QTY")) And (rowstb1.Item("PART_NUM")) = (rowstb2.Item("PART_NUM"))) Into Count()) = 0).ToArray

I know this will appear as a long strand of code on the computer so here is a snippet of the aggregate portion of the code so you can read it easier:

(Aggregate rowstb1 As DataRow In dsOld.Tables("parts").Rows.OfType(Of DataRow)() Where ((rowstb1.Item("TRANSACTION")) = (rowstb2.Item("TRANSACTION")) And (rowstb1.Item("DESCRIPTION")) = (rowstb2.Item("DESCRIPTION")) And (rowstb1.Item("QTY")) = (rowstb2.Item("QTY")) And (rowstb1.Item("PART_NUM")) = (rowstb2.Item("PART_NUM"))) Into Count())

I researched the aggregate clause and know the basics of how it functions. However, I am having a tremendous amount of trouble converting it to C#. Any help will be greatly appreciated.

Upvotes: 0

Views: 131

Answers (2)

Adam Gritt
Adam Gritt

Reputation: 2674

What you are doing appears to be equivalent of a SQL LEFT JOIN operation where you want to find elements in the First collection that are not in the Second. You can do this with out having to use an aggregate by using the following:

IEnumerable<DataRow> newRows = dsNew.Tables["parts"].Rows.OfType<DataRow>();
IEnumerable<DataRow> oldRows = dsOld.Tables["parts"].Rows.OfType<DataRow>();

DataRow[] rowsNotFound = newRows
.GroupJoin(oldRows, 
    o => new 
        { 
            Transaction = o.Field<int>("TRANSACTION"), 
            Description = o.Field<string>("DESCRIPTION"), 
            Quantity = o.Field<int>("QTY"), 
            PartNumber = o.Field<string>("PART_NUM") 
        },
    i => new 
        { 
            Transaction = i.Field<int>("TRANSACTION"), 
            Description = i.Field<string>("DESCRIPTION"), 
            Quantity = i.Field<int>("QTY"), 
            PartNumber = i.Field<string>("PART_NUM") 
        },
    (o, i) => new {NewRow = o, OldRows = i})
.SelectMany(g => g.OldRows.DefaultIfEmpty(), (g, oldRow) => oldRow == null ? g.NewRow : null)
.Where(r => r != null)
.ToArray();

I didn't know the data types so I guessed based on the field names.

Upvotes: 1

Dave Doknjas
Dave Doknjas

Reputation: 6542

I think it'll be something like:

DataRow[] rowsnotfound = (
    from DataRow rowstb2 in dsNew.Tables("parts").Rows.OfType<DataRow>()
    where ((
            from DataRow rowstb1 in dsOld.Tables("parts").Rows.OfType<DataRow>()
            where (rowstb1["TRANSACTION"] == rowstb2["TRANSACTION"]
                && rowstb1["DESCRIPTION"] == rowstb2["DESCRIPTION"]
                && rowstb1["QTY"] == rowstb2["QTY"]
                && rowstb1["PART_NUM"] == rowstb2["PART_NUM"])
            select rowstb1).Count()) == 0).ToArray();

Upvotes: 0

Related Questions