Blog

Martin Martin
27.01.2016 20:57

Code-Snippet: LINQ - outsource where condition


Sometimes you need to look for specific objects in several linq queries. Therefore its very important to query the objects in the exactl same way in every query.

In this case, it makes sense to store the WHERE LINQ statments in a predicate variable. Then you can reuse it in all linq queries.

This sample code is simplefied and for demonstration:

Predicate<DateTime> dateTimePredicate = b => b.Hour == 1;
 
        List<DateTime> dateTimeList = new List<DateTime>()
        {
            DateTime.Now,
            DateTime.UtcNow,
        };
 
dateTimeList.Where(a=>dateTimePredicate.Invoke(a));

: LINQ, Where, Predicate
: 27. Jšnner 2016 21:18
Link zu diesem Artikel: (in die Zwischenablage)