LINQ and ToString

fpoyavo
fpoyavo used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2011
Top Expert 2015

Commented:
What error do you receive?

Author

Commented:
LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression
Most Valuable Expert 2011
Top Expert 2015

Commented:
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.
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Retired
Distinguished Expert 2017
Commented:
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

Most Valuable Expert 2011
Top Expert 2015

Commented:
Isn't that what I said?
Fernando SotoRetired
Distinguished Expert 2017

Commented:
@kaufmed, yes you did but I did not see your post until after I had posted mine.

Author

Commented:
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.
Fernando SotoRetired
Distinguished Expert 2017

Commented:
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.

Author

Commented:
Why would I do conversion to string if it was datetime? Your code does not help.
Fernando SotoRetired
Distinguished Expert 2017

Commented:
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?

Author

Commented:
I recommend you to read my question again.
Fernando SotoRetired
Distinguished Expert 2017

Commented:
Ok, what is ment by this, "I have query which works fine but only when I don't use ToString()"' ?
Top Expert 2016

Commented:
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?

Author

Commented:
Ok so may you change my code to show how to make it work please? :)

Author

Commented:
Fernando yes I manipulate it as string that was clear from question I hope.
Fernando SotoRetired
Distinguished Expert 2017

Commented:
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?

Author

Commented:
Hi Fernando,

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

Thanks.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial