The most efficient way to compare variables? WITH#

I get client data from MySql database and client object from web service.

I want to compare each value in a datatable with the values ​​in an object, and if there is one field that is different, I want to perform some tasks.

I know I can get values ​​from datatable with:

string mCompanyName = row["Company Name"].ToString();
string mCreatedDate = row["Created Date"].Tostring();
//etc..

Then I get the values ​​from the web service

string wsCompanyName = customer.companyName;
string wsCreatedDate = customer.createdDate;

There are about 50 fields and makes

if( mCompanyName != wsCompanyName & mCreatedDate != wsCreatedDate and so on..) (or similar)
{
    //Do something
}

seems a little tedious and not very pleasant, so how do I do this? Is there a better way to list it and use some fancy LINQ?

Thanks in advance.

+3
source share
3 answers

I would put them in a dictionary and search in this way:

Dictionary<string, string> mData = new Dictionary<string, string>();

mData.Add("Company Name", row["Company Name"].ToString());


Dictionary<string, string> wsData = new Dictionary<string, string>();

wsData.Add("Company Name", customer.CompanyName);

Then do a loop:

foreach (KeyValuePair<string, string> pair in mData)
{
    if (wsData[pair.Key] == pair.Value) 
    { 
        // Do something
    }
}

, mData ( ) wsData .

. ( ).

+1

( "" ) IEnumerable ( " " ) SequenceEqual . Equals()'ity " ".

:

var equal = (new object[] { row["A"], row["B"] })
    .SequenceEqual(new object[] { x.A, x.B });

LINQ, .

+2

Source: https://habr.com/ru/post/1790369/


All Articles