开发者

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&l开发者_开发技巧t;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...


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


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;
}


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.


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.


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

public void setdata (IQueryable mydata)
{
    textbox1.text = mydata.FirstOrDefault().PropertyName;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜