1
[HttpPost]
    public ActionResult RemoveCollaborationEntry(int collaborationEntryID = 0)
    {
        using (var collaborationEntriesContext = new CollaborationEntryContext())
        {
            collaborationEntriesContext.Collaborations.Remove(
                collaborationEntriesContext.Collaborations.ElementAtOrDefault(collaborationEntryID));

            collaborationEntriesContext.SaveChanges();               
        }

        return RedirectToAction(nameof(Index));
    }

When this method gets invoked, the exception is torwn:

LINQ to Entities does not recognize the method 'annaBevzenkoPortfolioMVC.AboutMe.CollaborationEntry ElementAtOrDefault[CollaborationEntry](System.Linq.IQueryable`1[annaBevzenkoPortfolioMVC.AboutMe.CollaborationEntry], Int32)' method, and this method cannot be translated into a store expression.

I am pretty new to Entity.. can someone help me?

3
  • Make sure you bring in the System.Linq namespace so it can find the extension method ElementAtOrDefault. Commented May 5, 2016 at 19:40
  • @BradleyUffner I've checked that. No problems with that... Commented May 5, 2016 at 19:41
  • 1
    Try to use .Where instead of ElementAtOrDefault Commented May 5, 2016 at 19:41

1 Answer 1

3

Entity Framewok does not know how to translate into SQL Query the follwoing method ElementAtOrDefault.

You need to use Find method by doing the code below:

collaborationEntriesContext.Collaborations.Find(collaborationEntryID)
Sign up to request clarification or add additional context in comments.

5 Comments

Really? Consider this link entityframeworktutorial.net/…. There is the example using ElementAt(int).
But this instruction studentList.ElementAt<Student>(0) is applied on a Linq TO Objects studentList not on a DbSet<Student> :)
var studentList = context.Students.ToList<Student>(); sudentList here is a collection of Object not and DbSet<Student>() because of ToList()
Mark it as an answer if this answer satisfied you, please :)
I am waiting for 2 minutes more to do that.

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.