Well, you could receive a delegate for getting the elements - that is, to make the action that is different:
public List<Item> GetItems(Func<Item, Repository> getOtherItems) {
if( getOtherItems == null ) {
throw new ArgumentException();
}
var result = new List<Item>();
foreach(var item in repo.GetItems2()) {
var x = getOtherItems();
if (x.Value > 5)
result.Add(x);
}
return result;
}
And then you could call it like so:
// In class A
this.GetItems( repo => repo.GetOtherItems1(item.Id, "param1", param2) );
// In class B
this.GetItems( repo => repo.GetOtherItems2(param1, param2, item.Id) );
But I notice that you also use repo.GetItems1()
in A and repo.GetItems2()
in B.
So the difference you pointed out is not the only one.
With this in mind, I would advise rethinking your class structure. These two could inherit from the same base class, and merely extend it. (Or it could be polymorphism, or...)
Maybe:
public interface IMyList<Item>
{
List<int> GetItems();
// If it returns a single item, the name should NOT be pluralized!!
Item GetOtherItems();
}
Or maybe an abstract class?
edit: read @JoeGeeky 's comment on the performance impact of using delegates; this may become relevant if the delegate is used on a very intensive cycle or under high loads.