2

I have the following query:

var userQuizzes = from quiz in Context.Quizzes
                  select new DashboardQuiz
                  {
                      QuizId = quiz.Id,
                      Questions = quiz.Questions
                      QuestionExcerpt = quiz.QuizVersion.Questions.FirstOrDefault().QuestionText
                      // etc...
                  }

That's all well and good, but is it possible to call my own methods that perform logic on the data as the models are instantiated?

I have a method on my Quiz POCO class IsQuizActive() that determines from the values in Quiz if the quiz is active.

So for example:

var userQuizzes = from quiz in Context.Quizzes
                  select new DashboardQuiz
                  {
                      QuizId = quiz.Id,
                      Questions = quiz.Questions
                      QuestionExcerpt = quiz.QuizVersion.Questions.FirstOrDefault().QuestionText

                      // Custom method IsQuizActive() called here
                      ActiveQuiz = quiz.IsQuizActive()
                  }

I get an error saying that it can't be converted to LINQ, which is understandable, but I created DashboardQuiz to use as my View Model.

As it currently stands, I have to loop through my objects and create another model DashboardQuizViewModel that can be used as a view model for each item:

var userQuizzes = from quiz in Context.Quizzes
                  select new DashboardQuiz
                  {
                      QuizId = quiz.Id,
                      Questions = quiz.Questions
                      QuestionExcerpt = quiz.QuizVersion.Questions.FirstOrDefault().QuestionText
                      // I'd like to call IsQuizActive() here
                  }

List<DashboardQuizViewModel> responseModel = new List<DashboardQuizViewModel();

foreach (var dashboardQuiz in userQuizzes)
{
    DashboardQuizViewModel viewModel = new DashboardQuizViewModel();
    viewModel.QuizId = dashboardQuiz .id;
    viewModel.Questions = dashboardQuiz.Questions;
    viewModel.QuestionExcerpt = dashboardQuiz.QuestionExcerpt;

    // Call it here instead
    viewModel.ActiveQuiz = dashboardQuiz.IsQuizActive();

    responseModel.Add(viewModel);
}

return responseModel;

The only other 'tidy' way that I can think of, is to have IsQuizActive as a getter on my View Model and call that from within the view. But I really want the values to be evaluated before passing it to my view model

5
  • Yes, You can put your custom logic, but you need to write custom model defined functions for this. refer this link msdn.microsoft.com/en-us/library/vstudio/… stackoverflow.com/questions/22352312/… Commented Feb 25, 2015 at 9:30
  • Cool! The model defined function looks like the way to go? Are there any drawbacks to the user defined functions? Commented Feb 25, 2015 at 9:41
  • i think we can use user defined functions with EDMFunctionattribute blogs.msdn.com/b/efdesign/archive/2008/10/08/…. Commented Feb 25, 2015 at 9:43
  • stackoverflow.com/a/9970181/4073386 Basically does the same thing but you can save some code writing. Commented Feb 25, 2015 at 9:45
  • Oh noo... I've just realized that the model defined functions don't work for code first, as you define them in the EDMX... there is no EDMX for code first. :( Commented Feb 25, 2015 at 9:52

1 Answer 1

1

This cannot be done without creating a model defined function for sql as the query is converted to SQL and SQL does not recognise IsQuizActive as a function by default. You could possibly put the logic in the query but it is very untidy and inefficient. You could shorten the way you are looping through though. I think this should work (calling toList() executes the query and therefore you do not need to convert it to a different view model if you don't want to):

var userQuizzes = from quiz in Context.Quizzes
              select new DashboardQuiz
              {
                  QuizId = quiz.Id,
                  Questions = quiz.Questions
                  QuestionExcerpt =     quiz.QuizVersion.Questions.FirstOrDefault().QuestionText
                  // I'd like to call IsQuizActive() here
              }

var newUserQuizzes = userQuizzes.ToList().Select(x => {
    x.ActiveQuiz = x.IsQuizActive();
    return x;
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the neater version of the loop. I'll go for that for now as I can't do model defined functions in code first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.