Solved

LINQ and ToString

Posted on 2014-03-02
18
283 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
  • 8
  • 6
  • 3
  • +1
18 Comments
 
LVL 74

Expert Comment

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

Author Comment

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

Expert Comment

by:käµfm³d 👽
Comment Utility
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
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
Comment Utility
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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
Isn't that what I said?
0
 
LVL 62

Expert Comment

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

Author Comment

by:fpoyavo
Comment Utility
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 62

Expert Comment

by:Fernando Soto
Comment Utility
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
Comment Utility
Why would I do conversion to string if it was datetime? Your code does not help.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
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
Comment Utility
I recommend you to read my question again.
0
 
LVL 62

Expert Comment

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

Expert Comment

by:David Johnson, CD, MVP
Comment Utility
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
Comment Utility
Ok so may you change my code to show how to make it work please? :)
0
 
LVL 1

Author Comment

by:fpoyavo
Comment Utility
Fernando yes I manipulate it as string that was clear from question I hope.
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
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
Comment Utility
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
Comment Utility
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now