I have two tables with the following layout
username | displayname username| FirstName | surname
---------|------------ -----------------------------
foo test foo | andrew | blah
fa display
What I'm trying to do is display all of the displayname
entries in the left hand table on my page. However, if there is a match on the username
in the right hand table, I want the value test
to be replaced by andrew
.
Example out:
username | displayname
---------|------------
foo andrew
fa display
Everything is a string.
Below is my attempt at what I need but I've messed it up as its applying the firstname
value to every entry in my first table.
_users = UserService.GetAll().ToList();
_updates = UserUpdateService.GetAll().ToList();
foreach (var user in _users)
{
foreach (var update in _updates)
if (user.FirstName != "" && user.UserName == update.UserName)
{
update.DisplayName = user.FirstName;
}
else
{
update.DisplayName = user.UserName;
}
}
Is there a way to do this with a single LINQ query?