Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access data into IQueryable?

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+OneTimeEnumerable1[ORM_Linq.Articu]}`

I don't see any method like you tell me. I see Cast<>, Expression, ToString...

like image 435
Mark Comix Avatar asked Jan 29 '10 15:01

Mark Comix


2 Answers

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:

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

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

With the above change in place, 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 a single result use SingleOrDefault which will return a default value if no results are returned:
var result = mydata.SingleOrDefault();
if (result != null)
{
       textbox1.text = result.ProductName; // use the column name
}
else
{
    // do something
}

If you have multiple results then loop over them:

foreach (var item in mydata)
{
   string name = item.ProductName;
   int id = item.ProductId;
   // etc..
}
like image 79
Ahmad Mageed Avatar answered Oct 29 '22 10:10

Ahmad Mageed


First, you should be using a strongly-typed version of IQueryable. Say that your objects are of type MyObject and that MyObject has a property called Name of type string. Then, first change the parameter mydata to be of type IQueryable<MyObject>:

public void setdata (IQueryable<MyObject> mydata)

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:

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

Or, if you want to concatenate all the names:

public void setdata(IQueryable<MyObject> mydata) {
    string text = String.Join(", ", mydata.Select(x => x.Name).ToArray());
    textbo1.Text = text;
}
like image 29
jason Avatar answered Oct 29 '22 08:10

jason