# comparing two Ilists to get differences RSS

## 9 replies

Last post Jun 15, 2012 12:40 PM by nijhawan.saurabh

Participant

1494 Points

782 Posts

### comparing two Ilists to get differences

Dear All,

I have two lists of the same type and I am trying to compare it.

So I have written a comparer class as given below but it is taking an eneormous amount of time(more than 1 hour) to compare a few thousand items in each ILIST.

```public class MyComparer : CheckAndValidate, IEqualityComparer<IEnumerable<FiscalNoteInfo>>
{
#region IEqualityComparer<IEnumerable<FiscalNoteInfo>> Members
IList<FiscalNoteInfo> fcommon = new List<FiscalNoteInfo>();
public bool Equals(IEnumerable<FiscalNoteInfo> x, IEnumerable<FiscalNoteInfo> y)
{
//throw new NotImplementedException(); bool
//var sp_n_st = x.Where(e => y.Any(l => l.Direct == e.Direct && (l.FiscalNoteNumber == e.FiscalNoteNumber || l.FiscalNoteNumber == e.NFENum) && l.BusinessPlace == e.BusinessPlace && l.CompanyCode == e.CompanyCode));
//return false;
bool b = false;
// this is for sorting the lists
var lg = from element in x orderby element.Direct orderby element.FiscalNoteNumber orderby element.BusinessPlace orderby element.CompanyCode select element;
var lg1 = from element in y orderby element.Direct orderby element.FiscalNoteNumber orderby element.BusinessPlace orderby element.CompanyCode select element;

foreach (var itmn in lg)
{
foreach (var ele in lg1)
{
if (itmn.Direct == ele.Direct && (ele.FiscalNoteNumber == itmn.FiscalNoteNumber || ele.FiscalNoteNumber == itmn.NFENum) && ele.BusinessPlace == itmn.BusinessPlace && ele.CompanyCode == itmn.CompanyCode)
{
b = true;
break;
}
}

}
return b;

}
public void Equals(out IList<FiscalNoteInfo> fn)
{
fn = fcommon;
}

public int GetHashCode(IEnumerable<FiscalNoteInfo> obj)
{
throw new NotImplementedException();
}

#endregion```

So this is perhaps the worst way of comparing two lists.

I have also tried other things like

fiscalfn.Intersect(Sati_data_to_Sap).ToList<FiscalNoteInfo>();

this returns an Ilist of count 0.

Which is the best way to do the compare to get differences?

SUBHRANIL BASU RAY, Mumbai

All-Star

16420 Points

3173 Posts

### Re: comparing two Ilists to get differences

Is it taking time locally as well or just on the Web service?

All-Star

16420 Points

3173 Posts

### Re: comparing two Ilists to get differences

You can use the Except method.

```List<YourType> except = listA.Except(listB, StringComparer.OrdinalIgnoreCase);
//You can replace the last parameter with an IEqualityComparer<YourType> of your choosing```
`Details:`
`http://msdn.microsoft.com/en-us/library/system.linq.enumerable.except.aspx`

Participant

1494 Points

782 Posts

### Re: comparing two Ilists to get differences

Hi Saurabh,

This code is running from local machine but the getting data from other servers like SAP, oracle.

Can the except method work with the comparer I have created or I need to write different code for it.

If different code is reqd can u pls send it.Will it be any faster if invoked thru' the except method rather than thru' its own object.

SUBHRANIL BASU RAY, Mumbai

All-Star

16420 Points

3173 Posts

### Re: comparing two Ilists to get differences

Yes it can be used with the comparator, please check the link.The comparator can be passed as an optional argument.

Participant

1494 Points

782 Posts

### Re: comparing two Ilists to get differences

Hi,

Pls send me a working code which will give me the differences b/w the two lists with or w/o comparator and how to invoke it if with comparator.

thanks.

SUBHRANIL BASU RAY, Mumbai

Participant

1494 Points

782 Posts

### Re: comparing two Ilists to get differences

Is there no one in Microsoft ASP.NET forum who can give an efficient code that gets the differences b/w two Ilists of same type?

SUBHRANIL BASU RAY, Mumbai

All-Star

16420 Points

3173 Posts

### Re: comparing two Ilists to get differences

Did you give it a try with the link that i gave you for the except function?

did you face any challenges in doing so, please post the results/errors/problems.

Participant

1494 Points

782 Posts

### Re: comparing two Ilists to get differences

#### nijhawan.saurabh

Did you give it a try with the link that i gave you for the except function?

I tried but it works with strings only.

It didnt work with custom objects as in my case.

SUBHRANIL BASU RAY, Mumbai