Compare and check collections using LINQ

I have to compare 2 collections and check if the transactionId in the TransactionLevelCommentsCollection exists in the TransactionCommentsCollection. If exists throw an alert of the TransactionLinq.Key.

//Code

      For Each Record In (From TransactionCommentLinq In Me.TransactionCommentsCollection
                                    Join TransactionLinq In Me.TransactionsCollection On TransactionLinq.TransactionId Equals TransactionCommentLinq.TransactionId
                                    Join TaskLinq In Me.WorkflowsController.TasksController.TasksCollection On TaskLinq.TaskId Equals TransactionCommentLinq.TaskId
                                    Select TransactionLinq.Key,TransactionLinq.TransactionId, TaskLinq.TaskId Distinct)

// Have to check the values exists in TransactionLevelCommentsCollection here 
    Next

The record returns the transactionId and with that i have to check inside the TransactionLevelCommentsCollection

Answers


Dim rec = From TransactionCommentLinq In Me.TransactionCommentsCollection
          Join TransactionLinq In Me.TransactionsCollection On TransactionLinq.TransactionId Equals TransactionCommentLinq.TransactionId
          Join TaskLinq In Me.WorkflowsController.TasksController.TasksCollection On TaskLinq.TaskId Equals TransactionCommentLinq.TaskId
          Select TransactionLinq.Key,TransactionLinq.TransactionId, TaskLinq.TaskId Distinct

Since you want to compare values of TransactionId only:

Dim transIds = rec.Select(Function(r) r.TransactionId)

Now you get a list of TransactionId's from the long linq above. Let's assume the value type is same as in your TransactionLevelCommentsCollection's TransactionId

Use Intersect:

Dim idExist = transIds.Intersect(TransactionLevelCommentsCollection.Select(Function(x) x.TransactionId)

Now idExist will contains list of ids exist in both collections.

UPDATE BASED ON NEW REQUIREMENT

Since OP needs the Key instead of the TransactionID, there are 3 ways to get the Keys.

Method 1: Continued from the method shown above, get the Key value of items using:

For Each id In idExist 
    rec.First(Function(o) o.TransactionId = id).Key
Next

Method 2: The codes below is a totally rewritten (not related to the method shown above) and it will return rec objects from the LINQ above that has TransactionID that exist in TransactionLevelCommentsCollection:

Dim xrec = rec.Where(Function(o) TransactionLevelCommentsCollection.Exists(Function(tlc) tlc.TransactionId = o.TransactionId))

Well, actually this code will loop each TransactionLevelCommentsCollection items checking for TransactionId that match TransactionId in each rec items. The difference is it is done in 1 line of code.

Method 3: This one also a totally rewritten code, using join query:

Dim xrec = From r In rec
           From tlc In TransactionLevelCommentsCollection
           Where r.TransactionId = tlc.TransactionId
           Select r.TransactionId, r.Key

Now xrec contain list of items from rec with each corresponding TransactionID and Key properties where its TransactionID exists in TransactionLevelCommentsCollection.


Need Your Help

How are virtual functions and vtable implemented?

c++ polymorphism virtual-functions vtable

We all know what virtual functions are in C++, but how are they implemented at a deep level?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.