How to use LINQ or Lambda expressions to update matching items in two generic lists?

I wanted to find out if there is an "efficient" way to find all matching items in one generic list them update items in another generic list using a filter criteria.

Example:  There is a list of customers and some customers having a missing first name and last name.  In another list there is a listing of customer details that will sometimes contain the first name and last name for some of the customers with missing info from the first list.  

How could I loop through or find only the items in list 1 where the customer first name and customer last name is null and update the first name and last name values with customer first name and customer last name values from list 2 if the commonality between the lists is not Id but address, city, and state?  Is there a way that is most efficient to do this that is not a series of nested loops?

var filteredCustomerList1 = customerlist1.FindAll(c => c.FirstName == null && c.LastName == null)...
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
How about this:
var missing = from item1 in list1
              join item1 in list2
              on     new { item1.Address, item1.City, item1.State }
              equals new { item2.Address, item2.City, item2.State }
              where item1.FirstName == null && item1.LastName == null
              select item1;

foreach (var item in missing)
    var match = list2.Find(sought => sought.Address == i.Address && sought.City == i.City && sought.State == i.State);

    item.FirstName = match.FirstName;
    item.LastName = match.LastName;

Open in new window

käµfm³d 👽Commented:

The query finds the elements that have a matching address, but where FirstName and LastName are null. The foreach does the actual updating, in conjunction with a lambda to grab the matching record from list2.
Fernando SotoRetiredCommented:
Hi endrec;

Here is another solution.

List<Customer> CustomerList1 = new List<Customer>( );
List<Customer> CustomerList2 = new List<Customer>( );
var missingInfo = from c1 in CustomerList1
                  where c1.FirstName == null || c1.LastName == null
                  join c2 in CustomerList2 on new { c1.Street, c1.City, c1.State } equals new { c2.Street, c2.City, c2.State }
                  select new {c1, c2};
foreach( var c in missingInfo )
    if( c.c1.FirstName == null ) c.c1.FirstName = c.c2.FirstName;
    if( c.c1.LastName == null ) c.c1.LastName = c.c2.LastName;

// Now you can iterate through the List CustomerList1 and see that it is updated

// Define other methods and classes here
public class Customer
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Street { get; set; }
    public string City { get; set; }
    public string State { get; set; }

Open in new window

Fernando SotoRetiredCommented:
Hi endrec;

Just in cast you are interested the following Linq query using lambda expression is equivalent to the query in my last post.

var missingInfoLambda = CustomerList1.Where( c1 => c1.FirstName == null || c1.LastName == null )
				.Join( CustomerList2, 
				       c1 => new { c1.Street, c1.City, c1.State }, 
				       c2 => new { c2.Street, c2.City, c2.State }, 
				       ( c1, c2 ) => new { c1, c2 } 

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.