|
-
Sep 27th, 2010, 04:34 AM
#1
Thread Starter
Addicted Member
[RESOLVED] How to pass the result of the linq to textbox?
How can I pass the result of the linq to a textbox?
Code:
string search = txtSearchPO.Text;
IPSBLL.TransactionInfo dbInfo = new IPSBLL.TransactionInfo();
var q = from info in dbInfo.GetTransactionInfo()
where info.PONumber == search
select info;
txtPONumber.Text = q;
I need to access the column data something like this:
Code:
DataTable.Rows[index].columnName
How can I do that in LINQ?
-
Sep 27th, 2010, 06:30 AM
#2
Re: How to pass the result of the linq to textbox?
Try the
Code:
txtPONumber.Text = q.FirstOrDefault();
Please mark you thread resolved using the Thread Tools as shown
-
Sep 27th, 2010, 06:36 AM
#3
Re: How to pass the result of the linq to textbox?
FirstOrDefault can return null so you should check if it's null value.
also if GetTransactionInfo return an instance of a class then it should be
Code:
txtPONumber.Text = q.ProperyName.ToString();
if ProperyName is a string by default you can remove the "ToString()" part.
Last edited by motil; Sep 27th, 2010 at 06:46 AM.
* Rate It  If you Like it
__________________________________________________________________________________________
" Programming is like sex: one mistake and you’re providing support for a lifetime."
Get last SQL insert ID 
-
Sep 27th, 2010, 09:52 AM
#4
Re: How to pass the result of the linq to textbox?
Are you expecting multiple rows to be returned from the query? If so, outputting this directly to a TextBox doesn't really make sense. It won't display the results in a usable format.
Gary
-
Sep 27th, 2010, 07:51 PM
#5
Thread Starter
Addicted Member
Re: How to pass the result of the linq to textbox?
Yap I am expecting multiple rows result from the query of linq. I tried to pass it into a DataTable and here is my code:
Code:
IPSBLL.TransactionInfo db = new IPSBLL.TransactionInfo();
DataTable dtTransInfo = new DataTable();
dtTransInfo = db.GetTransactionInfo();
var q = from info in dtTransInfo.AsEnumerable()
where info.Field<string>("PONumber") == search
select info;
dtTransInfo = q.CopyToDataTable();
txtPONumber.Text = dtTransInfo.Rows[0].Field<string>("PONumber");
This one works!
But unfortunately, if I will try to modify my linq query like this:
Code:
var q = from info in dtTransInfo.AsEnumerable()
where info.Field<string>("PONumber") == search
select new {
TransactionInfo = info.Field<int>("TransactionInfoID"),
PONumber = info.Field<string>("PONumber")
};
The CopyToDataTable(); not valid anymore;
dtTransInfo = q.CopyToDataTable();
How can I fix this?
-
Sep 28th, 2010, 01:42 AM
#6
Re: How to pass the result of the linq to textbox?
Hey,
I am by no means an expert on LINQ, I have been meaning to read a book on it, but just haven't got round to it.
Having said that, I think the reason that you are seeing this is due to the inferred type of q, following each query. In the first LINQ query, you are returning a number of different rows, in which case, the inferred type has the CopyToDataTable() method. However, in the second, you are no longer returning a number of different rows from the query, instead, you are returning a new instance of a class, in which case, there isn't the CopyToDataTable() method.
If you run the debugger, and set a watch on q, in both cases, you should hopefully see what I mean.
Gary
-
Sep 28th, 2010, 07:50 PM
#7
Thread Starter
Addicted Member
Re: [RESOLVED] How to pass the result of the linq to textbox?
Thank you guys for the help, I have now a clear understanding...
-
Sep 29th, 2010, 01:10 AM
#8
Re: [RESOLVED] How to pass the result of the linq to textbox?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|