Answered by:
Getting actual value from a column in SQL BD table via linq

Question
-
Hi,
I need to be able to retrieve the value stored in a column called "ClientEmail" from a "Client" table in a "TEST" DB. I then need to be able to use this value to send out an email e.g:
The following Linq query retrieves the details I need:
var clientEmail = (from c in DB.Clients where c.ClientID == Convert.ToInt32(Session[Constants.SessionKeys.ClientID]) select new { c.ClientEmail }).FirstOrDefault(); string clientMailBox = clientEmail.ToString();
I then need to use the value store in the "clientMailBox" string variable in order to send out an email.
However the result of the query returns me both the name of the column and its value in the following format:
"{ ClientEmail = test.dep.team"@testemail.co.uk }
The actual value from that column is:
test.dep.team"@testemail.co.uk
I am expecting to get above value, however I am not and instead I am getting:
"{ ClientEmail = test.dep.team"@testemail.co.uk }
Is there anything I am missing from my query ?
I would be very grateful for any suggestions.
- Edited by monster597 Monday, September 12, 2016 10:33 AM
Monday, September 12, 2016 10:32 AM
Answers
-
in your query you construct anonymous object (select new { c.ClientEmail }) and then serialize it to string, that's why you get the result in such form. Use expression syntax for getting string variable instead of object:
string clientEmail = DB.Clients.Where(c.ClientID == Convert.ToInt32(Session[Constants.SessionKeys.ClientID])).Select(c => c.ClientEmail).FirstOrDefault();
Blog - http://sadomovalex.blogspot.com
Dynamic CAML queries via C# - http://camlex.codeplex.com- Proposed as answer by Patrick_Liang Friday, September 23, 2016 9:07 AM
- Marked as answer by Patrick_Liang Tuesday, October 11, 2016 7:39 AM
Monday, September 12, 2016 3:13 PM -
Hi,
Please try to use the code below:
var clientEmail =(from c in DB.Clients where c.ClientID == Convert.ToInt32(Session[Constants.SessionKeys.ClientID]) select c.ClientEmail).FirstOrDefault(); string clientMailBox = clientEmail.ToString();
Best Regards,
Dennis
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com- Proposed as answer by Patrick_Liang Friday, September 23, 2016 9:07 AM
- Marked as answer by Patrick_Liang Tuesday, October 11, 2016 7:39 AM
Tuesday, September 13, 2016 6:36 AM
All replies
-
in your query you construct anonymous object (select new { c.ClientEmail }) and then serialize it to string, that's why you get the result in such form. Use expression syntax for getting string variable instead of object:
string clientEmail = DB.Clients.Where(c.ClientID == Convert.ToInt32(Session[Constants.SessionKeys.ClientID])).Select(c => c.ClientEmail).FirstOrDefault();
Blog - http://sadomovalex.blogspot.com
Dynamic CAML queries via C# - http://camlex.codeplex.com- Proposed as answer by Patrick_Liang Friday, September 23, 2016 9:07 AM
- Marked as answer by Patrick_Liang Tuesday, October 11, 2016 7:39 AM
Monday, September 12, 2016 3:13 PM -
Hi,
Please try to use the code below:
var clientEmail =(from c in DB.Clients where c.ClientID == Convert.ToInt32(Session[Constants.SessionKeys.ClientID]) select c.ClientEmail).FirstOrDefault(); string clientMailBox = clientEmail.ToString();
Best Regards,
Dennis
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com- Proposed as answer by Patrick_Liang Friday, September 23, 2016 9:07 AM
- Marked as answer by Patrick_Liang Tuesday, October 11, 2016 7:39 AM
Tuesday, September 13, 2016 6:36 AM