Can't pass the proper value to the linq query variable.

Hi Experts,

ABCEntities Context = new ABCEntities();

                int val = int.Parse(workOrderDropDownList.SelectedValue.ToString());

                var woDateQuery = (from p in Context.WOes
                                  where p.WorkOrder == val
                                  select p.Date);
                                
                dateRequestedLabel.Text = woDateQuery.ToString();

Open in new window


When I run this query, instead of getting a date as anticipated, I get what appears to be a version of the query:

The dateRequestedLabel reads: "SELECT [Extent1].[Date] AS [Date] FROM [dbo].[WO] AS [Extent1] WHERE [Extent1].[WorkOrder] = @p__linq__0"

Thanks in advance for your help.

W
WhyDidntItWorkAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
Should have mentioned before . . . it doesn't know that p.WorkOrder (I'm guessing) is a unique / primary key.

woDateQuery is a queryResult, which could contain 1 or more records.  Imagine a status history table where you had columns date, status, workorder . . . linq doesn't know you're only expecting 1 result

try
                 
                dateRequestedLabel.Text = woDateQuery.FirstOrDefault().Date.ToString();

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Lokesh B RDeveloperCommented:
Hi,

Check query result for NULL and assign the value to avoid Error.

var woDateQuery = (from p in Context.WOes
                                  where p.WorkOrder == val
                                  select p.Date);

if(woDateQuery.FirstOrDefault() != null)
{
      dateRequestedLabel.Text = woDateQuery.FirstOrDefault().Date.ToString();
}

Open in new window

Fernando SotoRetiredCommented:
Hi WhyDidntItWork;

The query that you posted has not yet executed and the reason why you are seeing the query that will be sent to the SQL server, once you cause the query to be enumerated over that query will return a collection of objects and NOT a single instance of an object. If you are expecting a single object to be return then you need to pull that object out of the collection and assign it to a variable. The code snippet below should do what you need.

// The query retuning a single object of date or null
var woDateQuery = (from p in Context.WOes
                   where p.WorkOrder == val
                   select p.Date).FirstOrDefault();

// Set up a variable that will a string version of a DateTime object
string woDateStr = "Returned an empty Collection";
// Test to see if a value was returned and if so fill the woDateStr variable
if( woDateQuery != null )
{
    woDateStr = woDateQuery.ToString();
}

Open in new window

WhyDidntItWorkAuthor Commented:
Thanks for your help.  Much appreciated.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.