• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 853
  • Last Modified:

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

Hello,
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)...
0
endrec
Asked:
endrec
  • 2
  • 2
3 Solutions
 
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

0
 
käµfm³d 👽Commented:
P.S.

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.
0
 
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
0
 
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


Fernando
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now