Dave Gordon February 2016

EF 6 Skip all records where column is null, from results get row 'N'

I have the following EF Linq query:

var face = de.Faces.OrderBy(f => f.id).Skip(recordNumber)
              .FirstOrDefault(fd => fd.FuzzyHash != null);

What it is supposed to be doing, but failing is:

  1. Get Nth recordNumber from Faces which has an existing FuzzyHash

Could someone fix the query and explain the fix to me - what am I doing wrong?

Answers


Tommy February 2016

EF Linq operates much like you would think out this problem logically.

I need to get the nth record from an ordered list of Faces where some column is not null.

First, get your list of not null column Faces, then order, then skip, then get the record.

var face = de.Faces.Where(fd => fd.FuzzyHash != null).OrderBy(f =>
f.id).Skip(recordNumber).FirstOrDefault();


Sam Axe February 2016

You need to remove all records where FuzzyHash is null.

var face = de.Faces.Where(r => null != r.FuzzyHash).OrderBy(f => f.id).Skip(recordNumber)
          .FirstOrDefault();


Yacoub Massad February 2016

The current query would skip N records, and then take the first record that has a FuzzyHash.

What you want to do is first filter the records to include only the ones that have a FuzzyHash and then skip N records like this:

var face = de.Faces
    .Where(fd => fd.FuzzyHash != null)
    .OrderBy(f => f.id)
    .Skip(recordNumber) //shouldn't this be recordNumber - 1?
    .FirstOrDefault();

Post Status

Asked in February 2016
Viewed 2,946 times
Voted 12
Answered 3 times

Search




Leave an answer