2

I'm new to LINQ to SQL world - So I'm sorry if the question is stupid :) - I just could not find any answer on the internet:

I have a table with two columns - "Batch_Name" and "Batch_id". The user select the batch id and the application needs to return its name.

I have this C# Code to extract the batch name according to the batch id ("myBatchNum"):

        var thisBatch = from x in gridDB.batches
                        where x.batch_id == myBatchNum
                        select new { x.batch_name };


        lblBatchName.Text = thisBatch.First().ToString();

This extract from the proper batch name, but when I try to display the name on a label control, I get this result ("NightBatch is the name in the DB):

{batch_name = NightBatch }

How do I extract the batch name from "thisBatch" properly to the label control?

Thank you so much, Nim.

4 Answers 4

3

It's unclear that you need to use an anonymous type at all. Just use:

var thisBatch = from x in gridDB.batches
                where x.batch_id == myBatchNum
                select x.batch_name;

lblBatchName.Text = thisBatch.First();

Or if you did want to use an anonymous type still, just use the property you've effectively created:

var thisBatch = from x in gridDB.batches
                where x.batch_id == myBatchNum
                select new { x.batch_name };

lblBatchName.Text = thisBatch.First().batch_name;
Sign up to request clarification or add additional context in comments.

Comments

1

Remove the ToString() at the end as you already select only the name

Comments

1

You are creating an anonymous type with one property called batch_name. Chage your code as below to return strings:

var thisBatch = from x in gridDB.batches
                where x.batch_id == myBatchNum
                select x.batch_name;

Comments

0

You are calling ToString() on the anonymous type you've generated in the select statement. Try this:

lblBatchName.Text = thisBatch.First().batch_name;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.