?
Solved

LINQ and ToString

Posted on 2014-03-02
18
Medium Priority
?
295 Views
Last Modified: 2016-02-10
Hi Experts,

I have query which works fine but only when I don't use ToString()

Here it is and I need to make sure I can format Timestamp

myout.AddRange((from o in context.a
                                     join od in context.b on o.t_id equals od.id
                                     select new MyInfo
                                     {

                                         TId = od.id,
                                         //Timestamp = od.some_dte == null ? "" : od.some_dte.Value.ToString("yyyy/MM/dd HH:mm tt"),
                                        


                                     }).ToArray());

Open in new window



Thanks.
0
Comment
Question by:fpoyavo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
  • 3
  • +1
18 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39898940
What error do you receive?
0
 
LVL 1

Author Comment

by:fpoyavo
ID: 39898943
LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39898958
Since LINQ-to-SQL and Entity Framework ultimately translate your LINQ statements into actual SQL queries, you are bound by the confines of SQL. There is no SQL equivalent of ToString, so you'll need to execute that method outside of your EF query. Just follow up your query with another LINQ statement--one that executes only on your system and not on the database server.

e.g.

var query = (from o in context.a
             join od in context.b on o.t_id equals od.id
             select new MyInfo
             {
                 TId = od.id,
                 Timestamp = od.some_dte,
             }).ToArray()

var mapped = (from item in query
              select new
              {
                  TId = item.TId,
                  Timestamp = item.Timestamp == null ? "" : item.Timestamp.Value.ToString("yyyy/MM/dd HH:mm tt"),
              }).ToArray();

myout.AddRange(mapped);

Open in new window


The first ToArray call--the one that you originally had--will force the database query to execute. The second query therefore is working with data on your machine, and not on the database server.
0
Setting up LaraDock for Laravel

Learn how to set up LaraDock in a Laravel project - LaraDock gives us an easy way to run a Laravel application using Docker in a single command.

 
LVL 63

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 39898998
Hi fpoyavo;

I am going to assume that you are using linq to SQL or Linq to EF and in that dialect of Linq ToString is not supported and therefore you must format the string on the local machine as shown in the modified query below.

myout.AddRange((from o in context.a
                join od in context.b on o.t_id equals od.id
                select new
                {
                    od.id,
                    od.some_dte
                }).ToList()
                  .Select( od => new MyInfo
                   {
                       Tid = od.id,
                       Timestamp = od.some_dte == null 
                                     ? "" 
                                     : od.some_dte.Value.ToString("yyyy/MM/dd HH:mm tt")
                   }).ToArray());
                

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39899001
Isn't that what I said?
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39899006
@kaufmed, yes you did but I did not see your post until after I had posted mine.
0
 
LVL 1

Author Comment

by:fpoyavo
ID: 39899034
Ok. The problem is if I remove formating in first part it gives error. Timestamp in my class is string and column is datetime and that is why I used formatting in the first place :))

Please advise.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39899045
Did you try my solution, I think it should work seeming I did not use you custom class until the end where it is already a string.
0
 
LVL 1

Author Comment

by:fpoyavo
ID: 39899056
Why would I do conversion to string if it was datetime? Your code does not help.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39899063
To your question, "Why would I do conversion to string if it was datetime? Your code does not help.", because that is what you asked to do in the original question, has that changed?
0
 
LVL 1

Author Comment

by:fpoyavo
ID: 39899127
I recommend you to read my question again.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39899143
Ok, what is ment by this, "I have query which works fine but only when I don't use ToString()"' ?
0
 
LVL 82

Expert Comment

by:David Johnson, CD, MVP
ID: 39899173
The problem is if I remove formating in first part it gives error. Timestamp in my class is string and column is datetime and that is why I used formatting in the first place

why not using the date properties of the datetime ?  The database has it as a datetime object? But you manipulate it as a string?
0
 
LVL 1

Author Comment

by:fpoyavo
ID: 39899201
Ok so may you change my code to show how to make it work please? :)
0
 
LVL 1

Author Comment

by:fpoyavo
ID: 39899368
Fernando yes I manipulate it as string that was clear from question I hope.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39899376
So if Timestamp in your class MyInfo is a string then what is my posted solution not doing for you? Is it giving an exception?
0
 
LVL 1

Author Comment

by:fpoyavo
ID: 39899392
Hi Fernando,

You code is almost ok except I am having problem with another thing but it will be another new question.

Thanks.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question