Popular Posts

Wednesday, May 23, 2012

Compare two datatable using LINQ Query - CodeProject

The LINQ .Except function compares the references if you are trying to find rows that are in Table1 and not in Table2 and therefore never returns correct value. To get around this issue we need to create to tables with columns and then use them using the except:


        Dim allAccountsBrief = accounts.AsEnumerable().[Select](Function(a) New With
                                                                            {Key .AccountKey = a.Item("AccountKey").ToString()})
        Dim linkedAccountsBrief = _LinkedAccounts.AsEnumerable().[Select](Function(a) New With
                                                                            {Key .AccountKey = a.Item("AccountKey").ToString()})
 
        Dim unLinkedAccountsBrief = allAccountsBrief.Except(linkedAccountsBrief)
 
        Dim unLinkedAccountsTable = (From a In accounts.AsEnumerable
                                        Join unlnked In unLinkedAccountsBrief
                                            On a.Item("AccountKey").ToString Equals unlnked.AccountKey
                                        Select a
                                    ).CopyToDataTable


Compare two datatable using LINQ Query - CodeProject:

'via Blog this'

No comments:

Post a Comment