MvcMusicStore No elements in sequence

I am working through the Mvc Music Store tutorial and am stuck on this LINQ query as it keeps telling me that the sequence has no elements. My model matches the model in the tutorial and I have inserted data into it. I have modified it so instead of albums it is designs.

var genreModel = storeDB.DesignTypes.Include("Designs")
            .Single(g => g.Name == designType); -> no elements in sequence

var viewModel = new StoreBrowseViewModel() {
    Genre = genreModel,
    Albums = genreModel.Designs.ToList()
};

return View(viewModel);

Answers


use FirstOrDefault instead of Single:

var genreModel = storeDB.DesignTypes.Include("Designs")
            .FirstOrDefault(g => g.Name == designType);

From msdn:

Enumerable.Single Method Returns the only element of a sequence, and throws an exception if there is not exactly one element in the sequence.

You have more than one match item or no item.


Need Your Help

jQuery - passing input field to hidden input value

jquery input field

Im trying to use the jQuery .change function to get an input field value:

Mixing Linux and Windows MongoDB replica set and is equal hardware important for sharding

mongodb replication cluster-computing sharding

My first question is really simple, I just want to know if I can mix Linux and Windows mongodb servers in my sharding/replica cluster, ofc with same version like 2.2?