linq query to compare elements by element and find exact match

Jey 1 Reputation point
2021-12-01T17:37:41.79+00:00

hello all

in C#, i have 2 list array

List 1 has {Id, name, sex, dob} example { [100, Aaron, M, 2021-11-21] , [101, Billy, M, 2021-10-21], [102, Chris, F, 2021-11-28]
List 2 has {name, wardNumber} example { [R, W100], [Y, W100], [K, W100], [Billy, W100], [Z, W20], [Chris, W101],

Now, what i have to do is, I have to take Name of List1 and compare it with List 2, the moment when it match with List2 Name field then It has to stop search and return back values(name, wardNumber) from List2; which means once the perfect match found then have to stop search further since, I do have 1000's of data in List1 and List2.

I dont want to use for each so, is it possible by LINQ or Generic collections?

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,254 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Yijing Sun-MSFT 7,066 Reputation points
    2021-12-02T03:20:26.48+00:00

    Hi @Jey ,
    As far as I think,you could join two lists and select the value.Just like this:

         var test = from i in List2  
                           join j in List1 on i.name equals j.name  
                           select new { name = i.name, wardNumber = i.wardNumber };  
    

    Best regards,
    Yijing Sun


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments