0

Hello I have one List of List Which is dynamic means some times my list contains two lists or some time it will contais three lists like here we can say my List contais three lists.

list1.Add(new Schema() { High = 10, Low = 8, OpenValue = 7, Price = 8.5, Time = DateTime.Today.AddDays(-7), Volume = 234234232 });
list2.Add(new Schema() { High = 10, Low = 8, OpenValue = 7, Price = 8.5, Time = DateTime.Today.AddDays(-6), Volume = 234234232 });
list3.Add(new Schema() { High = 10, Low = 8, OpenValue = 7, Price = 8.5, Time = DateTime.Today.AddDays(-7), Volume = 234234232 });

and I have

List<List<Schema>> llsl = new List<List<Schema>>();
llsl.Add(list1);
llsl.Add(list2);
llsl.Add(list3);

now I want to compare List with each other like First I have to Compare list1 to list2 then list1 to list3 then list2 to list3 then list2 to list1 and so on so can anyone please help me how can I Achive it.

The purpose of doing it I want to final list which contais same no of Items let's us say my list1 Contain date 20 feb same my list3 also Contain a date 20feb but my list2 doesn't contain so I want final list which contains all three list but list2 with null value because it's not contains 20 feb so basically I want to compare dates in lists.

Thanks in Advance.

Dhaval Patel
  • 7,471
  • 6
  • 37
  • 70

1 Answers1

0

Your question is not much clear to me but If you wanted to make element null if it does not match with any other element in collection than here is how you go.

override a Equals and Hash method in your class called Schema as follow:

public class Schema
{
    public int High { get; set; }
    public int Low { get; set; }
    public int OpenValue { get; set; }
    public double Price { get; set; }
    public DateTime Time { get; set; }
    public int Volume { get; set; }
    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj)) return false;
        if (ReferenceEquals(this, obj)) return true;
        if (obj.GetType() != this.GetType()) return false;
        return Equals((Schema)obj);
    }
    protected bool Equals(Schema other)
    {
        //You may would like to compare only Date here.
        return High == other.High && Low == other.Low && OpenValue == other.OpenValue && Price.Equals(other.Price) && Time.Equals(other.Time) && Volume == other.Volume;
    }
    public override int GetHashCode()
    {
        unchecked
        {
            int hashCode = High;
            hashCode = (hashCode * 397) ^ Low;
            hashCode = (hashCode * 397) ^ OpenValue;
            hashCode = (hashCode * 397) ^ Price.GetHashCode();
            hashCode = (hashCode * 397) ^ Time.GetHashCode();
            hashCode = (hashCode * 397) ^ Volume;
            return hashCode;
        }
    }
}

Then create Extension method like follows.(Look at the comments for the logic).

public static class ListExtension
{
    public static List<List<T>> MatchList<T>(this List<List<T>> list) where T : class
    {
        //will contain matched list element index
        var matchedList = new List<Tuple<int, int>>();
        for (var i = 0; i < list.Count - 1; i++)
        {
            for (var j = i + 1; j < list.Count; j++)
            {
                //add element to matchedList if all are equal.
                var iElement = list.ElementAt(i);
                var jElement = list.ElementAt(j);
                if (iElement.Count != jElement.Count) continue;
                var flag = !iElement.Where((t, k) => !iElement.ElementAt(k).Equals(jElement.ElementAt(k))).Any();
                if (flag)
                {
                    //add element here.
                    matchedList.Add(new Tuple<int, int>(i, j));
                }
            }
        }

        var item1 = matchedList.Select(d => d.Item1).ToList();
        var item2 = matchedList.Select(d => d.Item2).ToList();
        //distinct elements that matchced perfectly.
        var sameGroup = item1.Union(item2);
        for (var i = 0; i < list.Count; i++)
        {
            if (!sameGroup.Contains(i))
            {
                //make it null where it did not matched as your requirement
                list[i] = null;
            }
        }
        //finally return the updated list.
        return list;
    }
}

Here is working fiddle for you in case you wanted to test and play with it.

Jenish Rabadiya
  • 6,708
  • 6
  • 33
  • 62