Solved

LINQ and ToString

Posted on 2014-03-02
18
291 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 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 80

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
 
LVL 1

Author Comment

by:fpoyavo
ID: 39899404
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

726 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