Link to home
Start Free TrialLog in
Avatar of Poolcorp
PoolcorpFlag for United States of America

asked on

LINQ to DataTable Update Question

Howdy, I am using C# and need a quick run down on how to do something with LINQ, at least I assume it can be done with LINQ. Basically here is what I need to do

I have a data table I have pulled from a server that has some GUIDs in it, I need to match those GUIDs to their respective GUIDS on my side and get the corresponding ID values, then in turn update my DataTable with said ID values.

So in other words I have a datatable that looks something like


Name     PhoneNumber      GUID        ID
Jim         123-456-7890      SDFSG  
Mark       234-424-1453     1GDE3  


and after I run said update process on it i will have


Name     PhoneNumber      GUID        ID
Jim         123-456-7890      SDFSG     234
Mark       234-424-1453     1GDE3     266

Help would be greatly appreciated. Thanks
Avatar of DaveJellison
DaveJellison
Flag of United States of America image

Linq is a query only language. Are you using Linq-to-SQL or the Entity framework perchance?

If not, you'll need to do your query to match the GUIDs, then just iterate your matches and update the database.

You're query will look something like this, or you could use a join, up to you...


List<WebUsers> managedUsers = new List<WebUsers>();

// pretend we have some users in memory here, or from a local
// database, whatever

var users = 
	from user1 in WebUsers
	from user2 in managedUsers
	where user1.GUID == user2.GUID
	select new WebUser
	{
		GUID = user1.GUID,
		PhoneNumber = user1.PhoneNumber,
		ID = user2.Id // managed (in-memory) ID
	};
	
foreach (var user in users)
{
	// update the database however you're currently doing so
}
	

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Poolcorp

ASKER

Thanks! Worked like a charm
Not a problem, glad I was able to help.  ;=)