0

I have List<object> and List<string> with records. I want inner join both list.

Following is my list with records.

var listContributionDetailsToBeUpdated = new List<ContributionDetailModel> 
{
    new ContributionDetailModel { EmployeeNumber="1", EmployeeFirstName="david", EmployeeLastName="k", NHIPNumber="nhi-100" },
    new ContributionDetailModel { EmployeeNumber="2", EmployeeFirstName="xavior", EmployeeLastName="f",NHIPNumber="nhi-101"},
    new ContributionDetailModel { EmployeeNumber="3", EmployeeFirstName="george", EmployeeLastName="s", NHIPNumber="nhi-102" },
    new ContributionDetailModel { EmployeeNumber="4", EmployeeFirstName="pandit", EmployeeLastName="h",NHIPNumber="nhi-103"},
    new ContributionDetailModel { EmployeeNumber="5", EmployeeFirstName="zania", EmployeeLastName="d", NHIPNumber="nhi-104" }
};

var updatedRecored = new List<string> { "nhi-100", "nhi-101", "nhi-102" };

Can any one help me to inner join both list.

2 Answers 2

3

It's pretty straight-forward. Just select NHIPNumber from contribution details as key for joining:

var result = from c in listContributionDetailsToBeUpdated
             join r in updatedRecored on c.NHIPNumber equals r
             select c;

Method syntax:

var result = listContributionDetailsToBeUpdated
     .Join(updatedRecored, c => c.NHIPNumber, r => r, (c, r) => c);

You can also use filtering instead of joining, but it's not optimal way, because join is a set operation and it uses internal lookup for joined collection. With simple filtering you will have complexity O(N*M) instead of O(N+M)

var result = listContributionDetailsToBeUpdated
     .Where(c => updatedRecored.Contains(c.NHIPNumber));
Sign up to request clarification or add additional context in comments.

2 Comments

Can any one help me to get all records Except "updatedRecored".Updated records is var updatedRecored = new List<string> { "nhi-100", "nhi-101", "nhi-102" };
@Vijeeshkumarvofox that should be another question :) simplest solution .Where(c => !updatedRecored.Contains(c.NHIPNumber)). For better performance you can use group join (i.e. join ... into g) and filter by where !g.Any() or use hashset instead of list for npi numbers.
0

The following may also work:

var result = listContributionDetailsToBeUpdated.Where(x => updatedRecored.Contains(x.NHIPNumber));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.