开发者

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开发者_Python百科 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);


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜