如何将数据访问到 IQueryable 中?
我有 IQueryable 对象,我需要将 IQueryable 内的数据放入 Textboxs 控件中。这可能吗?
我尝试类似的操作:
public void setdata (IQueryable mydata)
{
textbox1.text = mydata.????
}
更新:
我正在这样做:
public IQueryable getData(String tableName, Hashtable myparams)
{
decimal id = 0;
if (myparams.ContainsKey("id") == true)
id = (decimal)myparams["id"];
Type myType= Type.GetType("ORM_Linq." + tableName + ", ORM_Linq");
return this.GetTable(tableName , "select * from Articu where id_tipo_p = '" + id + "'");
}
public IQueryable<T> GetTable<T>(System.Linq.Expressions.Expression<Func<T, bool>> predicate) where T : class
{
return _datacontext.GetTable<T>().Where(predicate);
}
这返回 {System.Data.Linq.SqlClient.SqlProvider+OneTimeEnumerable
1[ORM_Linq.Articu]}`
我没有看到任何方法就像你告诉我的那样。我看到 Cast<>、Expression、ToString...
I have IQueryable object and I need to take the data inside the IQueryable to put it into Textboxs controls. Is this possible?
I try something like:
public void setdata (IQueryable mydata)
{
textbox1.text = mydata.????
}
Update:
I'm doing this:
public IQueryable getData(String tableName, Hashtable myparams)
{
decimal id = 0;
if (myparams.ContainsKey("id") == true)
id = (decimal)myparams["id"];
Type myType= Type.GetType("ORM_Linq." + tableName + ", ORM_Linq");
return this.GetTable(tableName , "select * from Articu where id_tipo_p = '" + id + "'");
}
public IQueryable<T> GetTable<T>(System.Linq.Expressions.Expression<Func<T, bool>> predicate) where T : class
{
return _datacontext.GetTable<T>().Where(predicate);
}
This returns a {System.Data.Linq.SqlClient.SqlProvider+OneTimeEnumerable
1[ORM_Linq.Articu]}`
I don't see any method like you tell me. I see Cast<>, Expression, ToString...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
编辑:根据其他帖子中的附加信息进行更新...
您的
getData
方法返回 IQueryable 而不是强类型结果,这就是您最终转换它的原因。尝试将其更改为:您是否尝试从不同的表查询“Articu”?
完成上述更改后,您的代码可以重写如下:
If you have a single result use SingleOrDefault which will return a default value if no results are returned:
如果有多个结果,则循环它们:
EDIT: Updated based on additional info from your other posts...
Your
getData
method is returning IQueryable instead of a strongly typed result, which is why you end up casting it. Try changing it to:Are you trying to query for "Articu" from different tables?
With the above change in place, your code can be rewritten as follows:
If you have a single result use SingleOrDefault which will return a default value if no results are returned:
If you have multiple results then loop over them:
首先,您应该使用强类型版本的 IQueryable。假设您的对象属于
MyObject
类型,并且MyObject
有一个名为Name
且类型为string
的属性。然后,首先将参数mydata
更改为IQueryable
类型:然后我们可以编写这样的主体来实际获取一些数据。假设我们只想要查询的第一个结果:
或者,如果您想连接所有名称:
First, you should be using a strongly-typed version of
IQueryable
. Say that your objects are of typeMyObject
and thatMyObject
has a property calledName
of typestring
. Then, first change the parametermydata
to be of typeIQueryable<MyObject>
:Then we can write a body like so to actually get some data out of. Let's say that we just want the first result from the query:
Or, if you want to concatenate all the names:
嗯,顾名思义,实现 IQueryable 的对象是......可查询的!您需要编写一个 linq 查询来获取 IQueryable 对象的内部详细信息。在您的 linq 查询中,您将能够提取其数据并将其分配到您想要的任何位置 - 例如文本框。
这是学习 Linq 的一个很好的起点。
Well, as the name suggests, an object implementing IQueryable is... Queryable! You'll need to write a linq query to get at the internal details of your IQueryable object. In your linq query you'll be able to pull out its data and assign bits of it where ever you'd like - like your text box.
Here's a great starting place for learning Linq.
我认为当您使用 FoxPro 和 DataSet 时,您会发现同样的心理斗争。非常好的、强大的基于字符串的功能(用于查询的 SQL、访问表和列名称)在这些世界中不可用,而是被一组已编译的强类型功能所取代。
如果您要根据编译时已知的数据源静态定义搜索和结果显示的 UI,这非常好。如果您尝试构建一个附加到仅在运行时已知并由配置数据定义的现有数据源的系统,那就不太好了。
I think you find the same mental struggle when coming from FoxPro and from DataSet. Really nice, powerful string-based capabilities(sql for query, access to tables and columns name) in these worlds are not available, but replaced with a compiled, strongly-typed set of capabilities.
This is very nice if you are statically defining the UI for search and results display against a data source known at compile time. Not so nice if you are trying to build a system which attaches to existing data sources known only at runtime and defined by configuration data.
如果您只需要一个值,只需调用 FirstOrDefault() 方法。
If you expect only one value just call
FirstOrDefault()
method.