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;
fcommon.Add(itmn);
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.
List<YourType> except = listA.Except(listB, StringComparer.OrdinalIgnoreCase);
//You can replace the last parameter with an IEqualityComparer<YourType> of your choosing
sunny74
Participant
1494 Points
782 Posts
comparing two Ilists to get differences
Jun 14, 2012 12:01 PM|LINK
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; fcommon.Add(itmn); break; } } } return b; } public void Equals(out IList<FiscalNoteInfo> fn) { fn = fcommon; } public int GetHashCode(IEnumerable<FiscalNoteInfo> obj) { throw new NotImplementedException(); } #endregionSo 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?
Pls reply asap.
Thanks for your efforts.
PLS MARK MY REPLY "AS ANSWER" IF IT HELPED YOU.
nijhawan.sau...
All-Star
16420 Points
3173 Posts
Re: comparing two Ilists to get differences
Jun 14, 2012 12:03 PM|LINK
Is it taking time locally as well or just on the Web service?
nijhawan.sau...
All-Star
16420 Points
3173 Posts
Re: comparing two Ilists to get differences
Jun 14, 2012 12:06 PM|LINK
You can use the Except method.
sunny74
Participant
1494 Points
782 Posts
Re: comparing two Ilists to get differences
Jun 14, 2012 01:03 PM|LINK
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.
Thanks for ur reply.
PLS MARK MY REPLY "AS ANSWER" IF IT HELPED YOU.
nijhawan.sau...
All-Star
16420 Points
3173 Posts
Re: comparing two Ilists to get differences
Jun 14, 2012 02:19 PM|LINK
Yes it can be used with the comparator, please check the link.The comparator can be passed as an optional argument.
sunny74
Participant
1494 Points
782 Posts
Re: comparing two Ilists to get differences
Jun 14, 2012 02:32 PM|LINK
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.
PLS MARK MY REPLY "AS ANSWER" IF IT HELPED YOU.
sunny74
Participant
1494 Points
782 Posts
Re: comparing two Ilists to get differences
Jun 15, 2012 08:54 AM|LINK
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?
PLS MARK MY REPLY "AS ANSWER" IF IT HELPED YOU.
nijhawan.sau...
All-Star
16420 Points
3173 Posts
Re: comparing two Ilists to get differences
Jun 15, 2012 09:30 AM|LINK
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.
sunny74
Participant
1494 Points
782 Posts
Re: comparing two Ilists to get differences
Jun 15, 2012 12:33 PM|LINK
I tried but it works with strings only.
It didnt work with custom objects as in my case.
PLS MARK MY REPLY "AS ANSWER" IF IT HELPED YOU.
nijhawan.sau...
All-Star
16420 Points
3173 Posts
Re: comparing two Ilists to get differences
Jun 15, 2012 12:40 PM|LINK
Here:
http://msdn.microsoft.com/en-us/library/system.linq.enumerable.except.aspx