Reputation: 995
I have two string collections and need to return any string value if the value doesn't exist in collection and index of value not equals. I implement with not exists condition, but I don't know add condition for index not equals.
public IEnumerable<string> GetInvalidHeaders(IEnumerable<string> list1, List<string> list2)
{
return list1.Where(header => list2 == null
|| list2.All(x => x != header));
}
Values of collections must unique by value and index
Upvotes: 0
Views: 3754
Reputation: 460098
This accepts not only strings and uses deferred execution:
public IEnumerable<T> GetDifferences<T>(IList<T> seq1, IList<T> seq2)
{
for (int i = 0; i < seq1.Count; i++)
{
T item1 = seq1[i];
if (i >= seq2.Count)
yield return item1;
else
{
T item2 = seq2[i];
if (!object.ReferenceEquals(item1, item2))
{
if (item1 == null || item2 == null)
yield return item1;
else if (!item1.Equals(item2))
yield return item1;
}
}
}
}
Usage:
var diff = GetDifferences(list1, list2);
Upvotes: 2
Reputation: 1276
public IEnumerable<string> GetDifferences(List<string> list1, List<string> list2)
{
for (int i = 0; i < list1.Count; i++)
{
if (list1[i] != list2[i]) yield return list1[i];
}
}
Do you need something like that ?
Upvotes: 4