I have some part of code. It's not real code but very similar with my problem in production.
Call of ASupplier in point 2 not compiled cause result of var filtered is List<IA>. It's look's like valid cause Where declared as
public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
But i can't understand why point 3 is valid because FixedIACondition declaration similar with result of call IAConditionCreator
using System;
using System.Collections.Generic;
using System.Linq;
namespace LinqWhereConditionProblem
{
internal class Program
{
public static void Main(string[] args)
{
var collection = new List<A>();
ASupplier(collection); // 1) OK
var filtered = collection.Where(IAConditionCreator("a")).ToList();
ASupplier(filtered); // 2) NOT OK
var filtered2 = collection.Where(FixedIACondition).ToList();
ASupplier(filtered2); // 3) OK
}
private static void ASupplier(IReadOnlyCollection<A> aCollection)
{
foreach (var a in aCollection)
{
Console.WriteLine(a.GetText());
}
}
private static Func<IA, bool> IAConditionCreator(string value)
{
return a => a.GetText() == value;
}
private static bool FixedIACondition(IA ia) => ia.GetText() == "aa";
}
public interface IA
{
string GetText();
}
public class A : IA
{
public string GetText()
{
return "ABC";
}
}
}