views:

684

answers:

6

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...

+2  A: 

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..
}
Ahmad Mageed
Don't you mean: string name = item.ProductName; int id = item.ProductId;
jinsungy
@jinsungy: thanks, that's right. Fixed :)
Ahmad Mageed
I use the same method for a lot of Tables, thats why i dont use strongly typed result. So, i have to cast every time, not?. Thanks
Mark Comix
@Mark yes, in that case you would need to cast as you were doing. If your SELECT statement is always going to represent an `ORM_Linq.Articu` object then you might as well make it `IQueryable<ORM_Linq.Articu>`. Nonetheless, you should follow the approach demonstrated above to capture the result using `SingleOrDefault()` to handle it correctly in case the result is null.
Ahmad Mageed
Thank you and everybody. Is really hard after 5 years working on Visual FoxPro learn how to work on C# and SQL :(
Mark Comix
@Mark if you haven't already then I suggest getting your hands on a good LINQ book. C#/SQL is one thing, LINQ to SQL is different than traditional ADO.NET using a `SqlConnection`. I recommend LINQ in Action by Fabrice Marguerie, Steve Eichert and Jim Wooley available here: http://www.manning.com/marguerie/ For C# I suggest Pro C# 2008 and the .NET 3.5 Platform (http://apress.com/book/view/1590598849) or C# 3.0 in a Nutshell (http://oreilly.com/catalog/9780596527570) - both these books have new 4.0 editions being released soon.
Ahmad Mageed
A: 

If you expect only one value just call FirstOrDefault() method.

public void setdata (IQueryable mydata)
{
    textbox1.text = mydata.FirstOrDefault().PropertyName;
}
Branislav Abadjimarinov
This should result in a compile error. FirstOrDefault method does not return a value of type string. Try, mydata.FirstOrDefault().PropertyName.
jinsungy
Yes, you're completely right, I've updated the answer.
Branislav Abadjimarinov
A: 

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.

Terry Donaghe
+1  A: 

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;
}
Jason
A: 

I do this nad Work. Opinions?

TextBoxCode.Text = mydata.Cast<ORM_Linq.Articu>().First().id.ToString();
TextBoxName.Text = mydata.Cast<ORM_Linq.Articu>().First().descrip; 

Really Thanks to everyone. Sorry for my poor English

Mark Comix
A: 

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.

Mike Graham