locked
Filter List with LINQ RRS feed

  • Question

  • User-173651909 posted

    I'm using EF6  in an ASP.net MVC app, and I need to filter some results from a table query.
    I can query a table with about 10 records and return all records into a variable:

    var results = db.mytable.Select(x => x.Total).ToList();

    This would return something like 10,20,30,40,50,60,70,80,90,100.
    I want to specify a value (for example 30) and then filter the list to consist of only that record (30) and the records on either side meaning the list should consist of 20, 30 and 40.

    How could this be performed?

    Regards
    Adam

    Monday, May 22, 2017 10:44 AM

All replies

  • User2103319870 posted

    I want to specify a value (for example 30) and then filter the list to consist of only that record (30) and the records on either side meaning the list should consist of 20, 30 and 40.

    You may need to consider implementing a Extension method which will find the previous and next item based on input value. You can find details sample implemention in this discussion

    Sample Implementation

    public static class LinqExtensions
        {
            public static IEnumerable<T> FindTriplets<T>(this IEnumerable<T> items, Predicate<T> matchFilling)
            {
                if (items == null)
                    throw new ArgumentNullException("items");
                if (matchFilling == null)
                    throw new ArgumentNullException("matchFilling");
    
                return FindTripletImpl(items, matchFilling);
            }
    
            private static IEnumerable<T> FindTripletImpl<T>(IEnumerable<T> items, Predicate<T> matchFilling)
            {
                using (var iter = items.GetEnumerator())
                {
                    T previous = default(T);
                    while (iter.MoveNext())
                    {
                        if (matchFilling(iter.Current))
                        {
                            yield return previous;
                            yield return iter.Current;
                            if (iter.MoveNext())
                                yield return iter.Current;
                            else
                                yield return default(T);
                            yield break;
                        }
                        previous = iter.Current;
                    }
                }
                // If we get here nothing has been found so return three default values
                yield return default(T); // Previous
                yield return default(T); // Current
                yield return default(T); // Next
            }
        }

    How to use above code

    AdventureWorksEntities db = new AdventureWorksEntities();
                //Get your results from database
                //change the query as per you design
                var results = db.Customers.Select(x=> x.CustomerID).Take(10).ToList();
    
                //change the input value 20 to any value you need
                var  triplets = results.FindTriplets(x => x== 20).ToList();

    Monday, May 22, 2017 4:14 PM