An explicit conversion exists (are you missing a cast?)

Getnews returns an IEnumerable<News> (i.e. multiple News) and you are trying to assign it to News news (i.e. a single News item). That doesn't work.

There are two possibilities, depending on what you want to do.

If you want to use all the news, change News news to IEnumerable<News>:

IEnumerable<News> news = newsService.Getnews(GroupID);

If you want to use only a single news, use FirstOrDefault:

News news = newsService.Getnews(GroupID).FirstOrDefault();

Depending on what you expect, you also could use one of the following:

  • First(): You expect Getnews to always return at least one news. This will throw an exception if no news are returned.
  • Single(): You expect Getnews to always return exactly one news. This will throw an exception if more than one or zero news are returned.
  • SingleOrDefault(): You expect zero or one news to be returned. This will throw an exception if more than one news are returned.

This line is setting a variable which is defined as a single instance of News to an instance of IEnumerable :

News news = newsService.Getnews(GroupID);

You want to change to

IEnumerable<News> = newsService.Getnews(GroupID);

Basically you are trying to set a collection of News to a single reference of News.


Getnews returns a collection of news items, and your line is expecting a single news item.

You could try

News news = newsServices.Getnews(GroupID).FirstOrDefault();

or expect an ineumerable

IEnumerable<News> news = newsService.GetNews(GroupID);

Tags:

C#

Asp.Net Mvc