C# 存在显式转换(您是否缺少演员表?)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16333557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:46:35  来源:igfitidea点击:

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

c#asp.net-mvc

提问by Aida E

I have a method that gives me the groupID of users and then I want to get the news based on the user's GroupID.

我有一种方法可以为我提供用户的 groupID,然后我想根据用户的 GroupID 获取新闻。

public IEnumerable<News> Getnews(int GroupID)
{
    Expression<Func<News, bool>> constraint = null;
    constraint = e => e.GroupID.Equals(GroupID);
    return newsRepository.GetMany(constraint);

}

here I call the above method:

这里我调用上面的方法:

News news = newsService.Getnews(GroupID);

and this is the error :

这是错误:

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<MeetingBoard.Model.News>' to 'MeetingBoard.Model.News'. An explicit conversion exists (are you missing a cast?)

无法将类型“ System.Collections.Generic.IEnumerable<MeetingBoard.Model.News>”隐式转换为“ MeetingBoard.Model.News”。存在显式转换(您是否缺少演员表?)

采纳答案by Bob Vale

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

Getnews 返回一组新闻条目,而您的线路正在等待单个新闻条目。

You could try

你可以试试

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

or expect an ineumerable

或者期待一个数不清的

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

回答by Paulie Waulie

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

这一行设置了一个变量,该变量定义为 News 的单个实例到 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.

基本上,您正在尝试将新闻集合设置为新闻的单个引用。

回答by Daniel Hilgarth

Getnewsreturns 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.

Getnews返回一个IEnumerable<News>(即多个新闻)并且您正试图将其分配给News news(即单个新闻项目)。那行不通。

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

有两种可能性,这取决于您想要做什么。

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

如果要使用所有新闻,请更改News newsIEnumerable<News>

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

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

如果您只想使用单个新闻,请使用FirstOrDefault

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

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

根据您的期望,您还可以使用以下方法之一:

  • First(): You expect Getnewsto always return at least one news. This will throw an exception if no news are returned.
  • Single(): You expect Getnewsto 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.
  • First(): 你期望Getnews总是至少返回一个消息。如果没有返回消息,这将引发异常。
  • Single():您希望Getnews始终只返回一个消息。如果返回多于一个或零个新闻,这将引发异常。
  • SingleOrDefault():您希望返回零个或一个消息。如果返回多个新闻,这将引发异常。

回答by MeTitus

This is what you want

这就是你想要的

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

or maybe something like:

或者可能是这样的:

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

回答by Ahmed KRAIEM

return newsRepository.GetMany(constraint);returns an IEnumerable<News>, you should do:

return newsRepository.GetMany(constraint);返回一个IEnumerable<News>,你应该这样做:

 return newsRepository.GetMany(constraint).FirstOrDefault();

return the first Newsif it is found in newRepository, null otherwise

News如果在 中找到,则返回第一个newRepository,否则为null

回答by Suraj Tiwari

An explicit conversion exists (are you missing a cast?) I was facing same problem in my C# code. I was writing code in VS. I checked full code, and I get reason behind this error. I missed 'I' in my explicit Interface name.

存在显式转换(您是否缺少强制转换?)我在 C# 代码中遇到了同样的问题。我在 VS 中编写代码。我检查了完整的代码,我明白了这个错误背后的原因。我在显式接口名称中遗漏了“I”。

Picture with Error

有错误的图片

Picture with Error

有错误的图片

Error Solution

错误解决方案

Error Solution

错误解决方案