-3
public static List<TDuplicate> ValidateColumnInList<TItem, TDuplicate>(List<TDuplicate> DuplicateExpression) where TDuplicate : DuplicateExpression
{
    List<TDuplicate> TempDuplicateExpression = new List<TDuplicate>();
    var itemProperties = typeof(TItem).GetProperties();
    foreach (var DplExpression in DuplicateExpression)
    {
        bool IsContainColumn = itemProperties.Any(column => column.Name == DplExpression.ExpressionName);
        if (!IsContainColumn)
        {
            TempDuplicateExpression.Add(DplExpression);
        }
    }
    return TempDuplicateExpression;
}

In the above section how to replace above foreach to linq ForEach.

Salah Akbari
  • 38,126
  • 10
  • 70
  • 102
Pradeep
  • 97
  • 2
  • 8
  • 2
    Possible duplicate of [LINQ equivalent of foreach for IEnumerable](http://stackoverflow.com/questions/200574/linq-equivalent-of-foreach-for-ienumerablet) – sr28 Aug 26 '16 at 07:47
  • your slightly misunderstanding, Linq is a query language for selecting data it has no ForEach, the List class does have a for each as well as the Parallel class but neither of them is Linq – MikeT Aug 26 '16 at 09:29

2 Answers2

0

You do not need foreach or ForEach here. Below code should give you result:

var itemProperties = typeof(TItem).GetProperties();
List<TDuplicate> tempDuplicateExpression = DuplicateExpression
    .Where(m => !itemProperties.Any(column => column.Name == m.ExpressionName))
    .ToList();

return tempDuplicateExpression;
Adil Mammadov
  • 8,216
  • 4
  • 30
  • 58
0

You can get result by this simple way:

var result = DuplicateExpression.Where(n=>!itemProperties.Any(column => column.Name == n.ExpressionName)).ToList();

Or you can user ForEach like this:

DuplicateExpression.ForEach(n=>
    {
        bool IsContainColumn = itemProperties.Any(column => column.Name == n.ExpressionName);
        if (!IsContainColumn)
        {
            TempDuplicateExpression.Add(n);
        }
    }
)