Click here to Skip to main content
15,901,373 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have two lists...the first list from database(olddata) and the second list from browser(newdata)..i need to compare that the two lists and save changes into the database..how it is possible?
thankyou........
Posted

1 solution

You can use LINQ keyword EXCEPT for that

C#
var differences = list1.Except(list2);


See here[^] for more information.
 
Share this answer
 
v2
Comments
User-11630313 18-Jan-16 5:13am    
here i need to compare each and every object element not a row..using except keyword i tried but it returns complete mismatch row...
Herman<T>.Instance 18-Jan-16 5:44am    
Could you post your troublesome code?
User-11630313 18-Jan-16 5:55am    
private Banks SaveBankDetails()
{
Banks obj = new Banks();
obj.Bankid = Formats.ConvertToInt(Request.QueryString["bankid"]);
obj.Bankname = txtBankName.Text;
obj.Active = Formats.ConvertStringToBool(ddlActive.SelectedItem);
obj.Userid = Session["UserId"].ToString();
List<banks> newdata = new List<banks>();
newdata.Add(obj);
List<banks> olddata = (List<banks>)Session["BankDetails"];
List<dbaudit> ChangedData = new List<dbaudit>();

in above code i need compare the two lists are
newdata and olddata...
the differences in object values should be add to changeddata list...
Herman<T>.Instance 18-Jan-16 6:02am    
I have done a cast form DataRow once in the past.
My code:

var inTbl1 = procedureOne.Rows.Cast<datarow>();
var inTbl2 = procedureTwo.Rows.Cast<datarow>();
var tblNameDiffT1 = inTbl1.Select(x => x["Name"]);
if (procedureOne.Rows.Count > procedureTwo.Rows.Count)
{
var tblNameDiffT2 = inTbl2.Select(x => x["Name"]);
var DiffByNameT1T2 = tblNameDiffT1.Except(tblNameDiffT2);
ProcedureOne and Two are DataTables
User-11630313 18-Jan-16 6:09am    
thankyou for responding...yeah your code is wright but it represents complete tablerow i think...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900