user1512559
user1512559

Reputation:

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

Upvotes: 3

Views: 194

Answers (2)

ajakblackgoat
ajakblackgoat

Reputation: 2149

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.

Upvotes: 1

Prince
Prince

Reputation: 328

just do like this

var GetCollection=(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).Tolist();

Get another collection of TransactionCommentsCollection like var SearchList=Db.tblTransactionCommentsCollection.tolist()
Foreach(var Obj in GetCollection)
{
      for(int i=0;i<  SearchList.count();i++)
       {
              if(Obj.transactionId==SearchList.elementat(i).transactionId)
                  {
                        messageBox()/Alert(SearchList.elementat(i).transactionId);
                        break;
                   }
       }
}

I do not know It would serve you as you want but in this I have got two list. One is your result set and other in which you have to search the particular Item. This syntax is of c#. you can do this is VB and please Mark the answer useful if it works for you.

Upvotes: 0

Related Questions