How to access data in IQueryable?

I have an IQueryable object, and I need to take the data inside IQueryable to put it in the Textbox controls. Is it possible?

I am trying something like:

public void setdata (IQueryable mydata) { textbox1.text = mydata.???? } 

Update:

I'm doing it:

 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 do not see any method as you tell me. I see Cast <>, Expression, ToString ...

+6
linq iqueryable
source share
5 answers

EDIT: Updated based on additional information from your other posts ...

Your getData method returns an IQueryable instead of a strongly typed result, so you end up casting it. Try changing it to:

 public IQueryable<ORM_Linq.Articu> getData(...) 

Are you trying to query "Articu" from different tables?

With the above change, your code can be rewritten as follows:

 ORM_Linq.Articu result = mydata.SingleOrDefault(); if (result != null) { TextBoxCode.Text = result.id.ToString(); TextBoxName.Text = result.descrip; } 


If you have one result, use SingleOrDefault , which will return the default value if the results are not returned:
 var result = mydata.SingleOrDefault(); if (result != null) { textbox1.text = result.ProductName; // use the column name } else { // do something } 

If you have several results, then iterate over them:

 foreach (var item in mydata) { string name = item.ProductName; int id = item.ProductId; // etc.. } 
+6
source share

First, you must use a strongly typed version of IQueryable . Let's say that your objects are of type MyObject and that MyObject has a Name property of type string . Then first change the mydata parameter to type IQueryable<MyObject> :

 public void setdata (IQueryable<MyObject> mydata) 

Then we can write such a body to get some data. Let's say that we just want to get the first result from the query:

 public void setdata (IQueryable<MyObject> mydata) { MyObject first = mydata.FirstOrDefault(); if(first != null) { textbox1.Text = first.Name; } } 

Or, if you want to combine all the names:

 public void setdata(IQueryable<MyObject> mydata) { string text = String.Join(", ", mydata.Select(x => x.Name).ToArray()); textbo1.Text = text; } 
+1
source share

Well, as the name implies, an object that implements IQueryable, ... Queryable! You will need to write a linq query to get the internal data of your IQueryable. In your linq query, you can pull out your data and assign a bit wherever you want, just like your text box.

Here is a great place to learn Linq .

0
source share

I think that you find the same mental struggle when exiting FoxPro and DataSet. Really nice, powerful string capabilities (sql for querying, access to tables and column names) are not available in these worlds, but are replaced by a compiled, strongly typed set of features.

This is very nice if you statically define a user interface for searching and displaying results with a data source known at compile time. Not so nice if you are trying to create a system that connects to existing data sources that are known only at runtime and are determined by configuration data.

0
source share

If you expect only one value will simply call the FirstOrDefault() method.

 public void setdata (IQueryable mydata) { textbox1.text = mydata.FirstOrDefault().PropertyName; } 
-one
source share

All Articles