1

I have a call made to Simple.Data where I want to limit the columns that are being brought back. However I am hitting problems..

This works fine:

var db = Database.Open();

var questionIdRow = db.Question.FindByFriendlyId(friendlyId);

if (questionIdRow == null) return Guid.Empty;

return questionIdRow.QuestionId;

However, the following doesn't work (I get a Simple.Data.UnresolvableObjectException 'Column not found')

var db = Database.Open();

var questionIdRow = db.Question.FindByFriendlyId(friendlyId)
                               .Select(db.Question.QuestionId);

if (questionIdRow == null) return Guid.Empty;

return questionIdRow.QuestionId;

I was under the impression from the Simple.Data documentation that this was all that I needed to do to limit the selected columns. Note that the selection is simply selecting the same column that is referenced later on.

The actual exception is thrown on the var questionIdRow = line.

Can anybody give me some guidance?

Ash
  • 5,057
  • 7
  • 35
  • 49

1 Answers1

1

this is a common problem, and has actually led to FindBy being deprecated before we even get to 1.0. The problem is that FindBy returns a record straight away, so you can't continue to call query methods on it.

The correct approach is to call FindAllBy and end with a First or FirstOrDefault:

var db = Database.Open();

var questionIdRow = db.Question.FindAllByFriendlyId(friendlyId)
                           .Select(db.Question.QuestionId)
                           .FirstOrDefault();

if (questionIdRow == null) return Guid.Empty;

return questionIdRow.QuestionId;
Mark Rendle
  • 9,274
  • 1
  • 32
  • 58
  • Good enough answer for me, I was using FindBy because I assumed it would be more efficient than FindAllBy, but I guess I'll try it and see what sql it creates before making an ass out of you and me.. – Ash Jan 31 '13 at 23:35
  • 1
    The "FirstOrDefault" adds a "TOP 1" to the executed SQL, so it's as efficient as can be. – Mark Rendle Feb 01 '13 at 19:38