逛逛数据源是一个无效的类型'Linq查询绑定的GridView时 [英] Getting 'Data source is an invalid type' when binding Linq query to Gridview

查看:142
本文介绍了逛逛数据源是一个无效的类型'Linq查询绑定的GridView时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图绑定一个GridView到的LINQ to SQL查询的使用存储过程。当我运行的页面,我得到以下错误:

I'm trying to bind a gridview to a linq to sql query that's using a stored procedure. When I run the page, i get the following error:

数据源的类型无效。它必须是一个IListSource,IEnumerable的,或的IDataSource

var db = new TableItemDataContext();
var q = db.sp_SearchForItems("1","2","3","4"); 
GridView1.DataSource = q;
GridView1.DataBind();

任何想法?

推荐答案

使用了ToList()扩展方法将查询转换为项列表。

Use the ToList() extension method to convert the query to a list of items.

GridView1.DataSource = q.ToList();

这也将在运行转换时的查询,所以你可能想看看刚刚铸造的IEnumerable会工作的效果。

This will also have the effect of running the query at the time of conversion, so you may want to see if just casting to IEnumerable would work.

修改:澄清基于注释踪迹。该问题被证明是与SPROC的结构和LINQ的无法检测的返回值。更改存储过程按<一个href=\"http://stackoverflow.com/questions/521736/stored-procedure-linq-dmbl-file-unable-to-inter$p$pt-the-result-set\">http://stackoverflow.com/questions/521736/stored-procedure-linq-dmbl-file-unable-to-inter$p$pt-the-result-set允许LINQ检测之后,存储过程可以改变回来的模式。

Edit: to clarify based on the comment trail. The issue turns out to be with the construction of the SPROC and the inability of LINQ to detect the return value. Changing the SPROC as per http://stackoverflow.com/questions/521736/stored-procedure-linq-dmbl-file-unable-to-interpret-the-result-set allowed LINQ to detect the schema after which the SPROC could be changed back.

这篇关于逛逛数据源是一个无效的类型'Linq查询绑定的GridView时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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