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

How to properly display TeX strings in axes' datatips? (MATLAB hg2)

matlab textbox matlab-figure undocumented-behavior matlab-hg2

I have recently tried to run an old piece of code (written on hg1) on a new version of MATLAB (2015a) that has hg2.

Attach bootstrap dropdown to different element

javascript css angularjs twitter-bootstrap twitter-bootstrap-3

I'd like to choose which element my bootstrap dropdown aligns with when it is opened. Is this possible? I'm open to doing it programmatically with AngularJS or JavaScript.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.