Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

entity framework filter nested collection #2752


User avatar
0
sergey created

My entity have collection of other entities,

public abstract class CounterMeasurmentPoint : MeasurmentPoint
 {
        public  ICollection<CounterMeasurmentPointValue> CounterMeasurmentPointValues { get; set; }
        public CounterMeasurmentPointValue GetLastNotErrorValue()
        {
            return CounterMeasurmentPointValues.Where(p => p.IsError == false).OrderByDescending(p=>p.DateTime).FirstOrDefault();                
        }

    }

Every next value depends on previous.

var lastValue=GetLastNotErrorValue();
var newValue=CalculateNewValue(lastValue,newValueParameters)

It is very slowly to load all values every time i want to add new item, now i do like that

_counterMeasurePointRepository.GetAll()
                    .Select(r => new {
                        MeasurePont = r,
                        Values = r.CounterMeasurmentPointValues.Where(p => p.IsError == false).OrderByDescending(p=>p.DateTime).Take(1)
                    })
                    .ToList().Select(p => p.MeasurePont).ToList()

Is there is any more elegant way to filter nested collection?


1 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Have you tried using Include ?

    _counterMeasurePointRepository.GetAll().Include(e=> e.CounterMeasurmentPointValues.Where(p => p.IsError == false))