无法将类型'Program.Data.View'隐式转换为System.linq.iqueryable< Program.Data.View>. [英] Cannot implicitly convert type 'Program.Data.View' TO System.linq.iqueryable<Program.Data.View>.

查看:67
本文介绍了无法将类型'Program.Data.View'隐式转换为System.linq.iqueryable< Program.Data.View>.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标/问题::我正在尝试使用First或FirstOrDefault从数据库中仅返回1个结果.我收到以下错误:

Goal/Problem: I am trying to use the First or FirstOrDefault to only return 1 result from the Database. I'm getting the following error:

不能将类型'Program.Data.view'隐式转换为System.Linq.Iqueryable存在显式转换(您是否缺少类型转换)

我尝试过的事情: 在浏览了文档和许多SO文章之后,我尝试了不同的转换方式,包括下面的代码. 之类的文章无法隐式转换类型'System.Linq.可查询到.存在显式转换(您是否缺少演员表?).大多数文章都是从System.Linq.IQueryable<>转到其他内容,而不是这个方向.无论哪种方式,投放都应该相对容易,我想念的是什么?:

What I've tried: After looking through documentation and many SO articles, I tried different ways of casting, including the code below. Articles such as Cannot implicitly convert type 'System.Linq.IQueryable<>to . An explicit conversion exists (are you missing a cast?). Most of the articles are going from System.Linq.IQueryable<> to something else, not this direction. Either way casting should be relatively easy, what am I missing?:

IQueryable<Program.Data.view> someVariable = db.view.First(m => m.ID == Search_ID);

我的方法签名是:

public IQueryable<Program.Data.view> GetDataFromQuery()

推荐答案

最终的工作如下:

var someVariable = db.view.Where(m => m.Some_ID == Search_SomeID).GroupBy(x => x.Some_ID).Select(g => g.OrderByDescending(p => p.Column_Two).FirstOrDefault()).AsQueryable().OrderBy(x => x.Column_Three);

又名(格式):

Where().GroupBy().Select(...OrderByDescending().FirstOrDefault()).AsQueryable().OrderBy();

不知道为什么其他人给我错误,但是按此顺序工作是可行的 另外,从逻辑上看,OrderBy在最后似乎完全没有必要,但是当我没有OrderBy时,会出现System.NotSupportedException错误(仅对于LINQ to Entities中的已排序输入,才支持方法"Skip".方法"OrderBy" '必须在方法'Skip'之前调用... ...这发生在.DataBind())

Not sure why the others gave me errors but doing it in this order worked Also, the OrderBy at the very end seems completely unecessary by logic, but I get a System.NotSupportedException error when I don't have it (The method 'Skip' is only supported for sorted input in LINQ to Entities. The method 'OrderBy' must be called before the method 'Skip' ... this happens on .DataBind())

这篇关于无法将类型'Program.Data.View'隐式转换为System.linq.iqueryable&lt; Program.Data.View&gt;.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆